JAVA写的NotePad
类别: JAVA教程
学了JAVA一个月.就写了个NotePad.由于时间关系.要实训了,很多功能没加上去,只实现了简单的界面和最基本上的功能.
以后有时间再完善吧..
=================================================================================
/*猫猫..第一个Java程序
*
*
*copyright 猫猫
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Notepad extends JFrame
{
String openFilePath;
String openFileName;
String title=\"ERROR MESSAGE\";
int type=JOptionPane.ERROR_MESSAGE;
public Notepad()
{
super(\"记事本\");
final JTextArea text = new JTextArea();
text.setToolTipText(\"请键入内容\");
//界面
//退出事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//简单的布局
final JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,1));
panel.add(new JScrollPane(text));
this.getContentPane().add(panel);
//菜单项
JMenuBar Mbar = new JMenuBar();
this.setJMenuBar(Mbar);
JMenu file = new JMenu(\"文件\");
JMenu edit = new JMenu(\"编辑\");
JMenu help = new JMenu(\"帮助\");
Mbar.add(file);
Mbar.add(edit);
Mbar.add(help);
JMenuItem newFile = new JMenuItem(\"新建\");
newFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.setText(\" \");
}
});
//布局结束
//新建文件
newFile.setMnemonic(\'N\');
newFile.setAccelerator( KeyStroke.getKeyStroke(\'N\',java.awt.Event.CTRL_MASK,true));
//打开文件
JMenuItem open = new JMenuItem(\"打开\");
open.setMnemonic(\'O\');
open.setAccelerator(KeyStroke.getKeyStroke(\'O\',java.awt.Event.CTRL_MASK,true));
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser openfile = new JFileChooser();
openfile.setDialogTitle(\"打开文件\");
openfile.setApproveButtonText(\"打开\");
openfile.showOpenDialog(panel);
File filename = openfile.getSelectedFile();
StringBuffer strBF = new StringBuffer();
String error_message = \"Error\";
FileInputStream inputfile = null;
try{
char buffer[] = new char[1024];
inputfile = new FileInputStream(filename);
int len = 0;
FileReader in = new FileReader(filename.getAbsoluteFile());
while((len = in.read(buffer)) != -1)
{
strBF.append(buffer , 0 , len);
}
inputfile.close();
text.setText(strBF.toString());
String openfilename = filename.getName();
setTitle(openfilename);
}
catch(IOException ioEX)
{
JOptionPane.showMessageDialog(panel,error_message,title,type);
}
}});
//保存文件
JMenuItem save = new JMenuItem(\"保存\");
save.setMnemonic(\'S\');
save.setAccelerator(KeyStroke.getKeyStroke(\'S\',java.awt.Event.CTRL_MASK,true));
save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser savefile=new JFileChooser();
savefile.setApproveButtonText(\"保存\");
savefile.setDialogTitle(\"保存文件\");
savefile.showSaveDialog(panel);
File filesa=savefile.getSelectedFile();
String file_notfound_message=\"找不到文件\";
FileOutputStream outputfile=null;
//处理异常开始
try
{
outputfile = new FileOutputStream(filesa);
}
catch(FileNotFoundException fe)
{
JOptionPane.showMessageDialog(panel,file_notfound_message,title,type);
}
String filecontent=text.getText();
String write_error_message=\"写文件错误\";
try
{
outputfile.write(filecontent.getBytes());
}
catch(IOException ioEx)
{
JOptionPane.showMessageDialog(panel,write_error_message,title,type);
}
String cmessage=\"关闭错误\";
try
{
outputfile.close();
}
catch(IOException ioEx)
{
JOptionPane.showMessageDialog(panel,cmessage,title,type);
}
}
}
);
//退出
JMenuItem exit = new JMenuItem(\"退出\");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
exit.setMnemonic(\'Q\');
exit.setAccelerator(KeyStroke.getKeyStroke(\'Q\',java.awt.Event.CTRL_MASK,true));
//查找
JMenuItem find = new JMenuItem(\"查找\");
find.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//因为课程太紧.所以查找功能没时间加上去了.^_^
}
});
find.setMnemonic(\'F\');
find.setAccelerator(KeyStroke.getKeyStroke(\'F\',java.awt.Event.CTRL_MASK,true));
//剪切
JMenuItem cut = new JMenuItem(\"剪切\");
cut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.cut();
}
});
cut.setMnemonic(\'C\');
cut.setAccelerator(KeyStroke.getKeyStroke(\'C\',java.awt.Event.CTRL_MASK,true));
//复制
JMenuItem copy = new JMenuItem(\"复制\");
copy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.copy();
}
});
copy.setMnemonic(\'o\');
copy.setAccelerator(KeyStroke.getKeyStroke(\'O\',java.awt.Event.CTRL_MASK,true));
//粘贴
JMenuItem paste = new JMenuItem(\"粘贴\");
paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.paste();
}});
paste.setMnemonic(\'P\');
paste.setAccelerator(KeyStroke.getKeyStroke(\'P\',java.awt.Event.CTRL_MASK,true));
JMenuItem about = new JMenuItem(\"关于\");
about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int type=JOptionPane.INFORMATION_MESSAGE;
String title=\"关于\";
String message=\"Make by cat lee\";
JOptionPane.showMessageDialog(panel,message,title,type);
}});
file.add(newFile);
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
help.add(about);
}
public static void main(String[] args) {
Notepad notepad = new Notepad();
notepad.setSize(640, 480);
notepad.setVisible(true);
notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
*
*
*copyright 猫猫
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Notepad extends JFrame
{
String openFilePath;
String openFileName;
String title=\"ERROR MESSAGE\";
int type=JOptionPane.ERROR_MESSAGE;
public Notepad()
{
super(\"记事本\");
final JTextArea text = new JTextArea();
text.setToolTipText(\"请键入内容\");
//界面
//退出事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//简单的布局
final JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,1));
panel.add(new JScrollPane(text));
this.getContentPane().add(panel);
//菜单项
JMenuBar Mbar = new JMenuBar();
this.setJMenuBar(Mbar);
JMenu file = new JMenu(\"文件\");
JMenu edit = new JMenu(\"编辑\");
JMenu help = new JMenu(\"帮助\");
Mbar.add(file);
Mbar.add(edit);
Mbar.add(help);
JMenuItem newFile = new JMenuItem(\"新建\");
newFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.setText(\" \");
}
});
//布局结束
//新建文件
newFile.setMnemonic(\'N\');
newFile.setAccelerator( KeyStroke.getKeyStroke(\'N\',java.awt.Event.CTRL_MASK,true));
//打开文件
JMenuItem open = new JMenuItem(\"打开\");
open.setMnemonic(\'O\');
open.setAccelerator(KeyStroke.getKeyStroke(\'O\',java.awt.Event.CTRL_MASK,true));
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser openfile = new JFileChooser();
openfile.setDialogTitle(\"打开文件\");
openfile.setApproveButtonText(\"打开\");
openfile.showOpenDialog(panel);
File filename = openfile.getSelectedFile();
StringBuffer strBF = new StringBuffer();
String error_message = \"Error\";
FileInputStream inputfile = null;
try{
char buffer[] = new char[1024];
inputfile = new FileInputStream(filename);
int len = 0;
FileReader in = new FileReader(filename.getAbsoluteFile());
while((len = in.read(buffer)) != -1)
{
strBF.append(buffer , 0 , len);
}
inputfile.close();
text.setText(strBF.toString());
String openfilename = filename.getName();
setTitle(openfilename);
}
catch(IOException ioEX)
{
JOptionPane.showMessageDialog(panel,error_message,title,type);
}
}});
//保存文件
JMenuItem save = new JMenuItem(\"保存\");
save.setMnemonic(\'S\');
save.setAccelerator(KeyStroke.getKeyStroke(\'S\',java.awt.Event.CTRL_MASK,true));
save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser savefile=new JFileChooser();
savefile.setApproveButtonText(\"保存\");
savefile.setDialogTitle(\"保存文件\");
savefile.showSaveDialog(panel);
File filesa=savefile.getSelectedFile();
String file_notfound_message=\"找不到文件\";
FileOutputStream outputfile=null;
//处理异常开始
try
{
outputfile = new FileOutputStream(filesa);
}
catch(FileNotFoundException fe)
{
JOptionPane.showMessageDialog(panel,file_notfound_message,title,type);
}
String filecontent=text.getText();
String write_error_message=\"写文件错误\";
try
{
outputfile.write(filecontent.getBytes());
}
catch(IOException ioEx)
{
JOptionPane.showMessageDialog(panel,write_error_message,title,type);
}
String cmessage=\"关闭错误\";
try
{
outputfile.close();
}
catch(IOException ioEx)
{
JOptionPane.showMessageDialog(panel,cmessage,title,type);
}
}
}
);
//退出
JMenuItem exit = new JMenuItem(\"退出\");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
exit.setMnemonic(\'Q\');
exit.setAccelerator(KeyStroke.getKeyStroke(\'Q\',java.awt.Event.CTRL_MASK,true));
//查找
JMenuItem find = new JMenuItem(\"查找\");
find.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//因为课程太紧.所以查找功能没时间加上去了.^_^
}
});
find.setMnemonic(\'F\');
find.setAccelerator(KeyStroke.getKeyStroke(\'F\',java.awt.Event.CTRL_MASK,true));
//剪切
JMenuItem cut = new JMenuItem(\"剪切\");
cut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.cut();
}
});
cut.setMnemonic(\'C\');
cut.setAccelerator(KeyStroke.getKeyStroke(\'C\',java.awt.Event.CTRL_MASK,true));
//复制
JMenuItem copy = new JMenuItem(\"复制\");
copy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.copy();
}
});
copy.setMnemonic(\'o\');
copy.setAccelerator(KeyStroke.getKeyStroke(\'O\',java.awt.Event.CTRL_MASK,true));
//粘贴
JMenuItem paste = new JMenuItem(\"粘贴\");
paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.paste();
}});
paste.setMnemonic(\'P\');
paste.setAccelerator(KeyStroke.getKeyStroke(\'P\',java.awt.Event.CTRL_MASK,true));
JMenuItem about = new JMenuItem(\"关于\");
about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int type=JOptionPane.INFORMATION_MESSAGE;
String title=\"关于\";
String message=\"Make by cat lee\";
JOptionPane.showMessageDialog(panel,message,title,type);
}});
file.add(newFile);
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
help.add(about);
}
public static void main(String[] args) {
Notepad notepad = new Notepad();
notepad.setSize(640, 480);
notepad.setVisible(true);
notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
-= 资 源 教 程 =-
文 章 搜 索