?? notepad.java
字號:
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="Designed by wenyouliang";
JOptionPane.showMessageDialog(panel,message,title,type);
}});
JMenuItem color = new JMenuItem("顏色");
color.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String title = "選取顏色";
Color co = JColorChooser.showDialog(panel,title, Color.black
);
text.setForeground(co);
}});
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);
edit.add(color);
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);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -