package test;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
public class Jsb implements ActionListener{
JFrame frame; //窗体
JMenuBar bar; //菜单栏
//菜单
JMenu menuFile,edit;
//文本框
JTextArea area;
//菜单项
JMenuItem newItem,openItem,savaItem,closeItem,cutItem,copyItem,pasteItem;
JFileChooser chooser;
File file;
public Jsb(){
frame = new JFrame("记事本");
bar = new JMenuBar();
menuFile = new JMenu("文件");
edit = new JMenu("编辑");
area = new JTextArea();
newItem = new JMenuItem("新建(N)");
openItem = new JMenuItem("打开(O)");
savaItem = new JMenuItem("另存为(A)");
closeItem = new JMenuItem("退出(X)");
cutItem = new JMenuItem("剪切(T)");
copyItem = new JMenuItem("复制(C)");
pasteItem = new JMenuItem("粘贴(P)");
frame.getContentPane().add(new JScrollPane(area)); //文本框加滚动条
//助记符
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
savaItem.setMnemonic(KeyEvent.VK_A);
closeItem.setMnemonic(KeyEvent.VK_X);
cutItem.setMnemonic(KeyEvent.VK_T);
copyItem.setMnemonic(KeyEvent.VK_C);
pasteItem.setMnemonic(KeyEvent.VK_P);
//快捷键
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,java.awt.Event.CTRL_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,java.awt.Event.CTRL_MASK));
savaItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,java.awt.Event.CTRL_MASK));
closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,java.awt.Event.CTRL_MASK));
cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,java.awt.Event.CTRL_MASK));
copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,java.awt.Event.CTRL_MASK));
pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,java.awt.Event.CTRL_MASK));
menuFile.add(newItem);
menuFile.add(openItem);
menuFile.add(savaItem);
menuFile.addSeparator();
menuFile.add(closeItem);
edit.add(cutItem);
edit.add(copyItem);
edit.add(pasteItem);
bar.add(menuFile);
bar.add(edit);
newItem.addActionListener(this);
openItem.addActionListener(this);
savaItem.addActionListener(this);
closeItem.addActionListener(this);
frame.setJMenuBar(bar);
frame.setLocation(500,250);
frame.setVisible(true);
frame.setSize(800,600);
}
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if(obj instanceof JMenuItem) {
JMenuItem item = (JMenuItem)obj;
if(item == newItem) { //新建
new Jsb();
}else if(item == openItem) { //打开
chooser = new JFileChooser();
chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
try {
FileInputStream fis = new FileInputStream(file);
byte b[] = new byte[fis.available()];
fis.read(b);
area.append(new String(b));
fis.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}else if(item == savaItem) { //另存为
chooser = new JFileChooser();
chooser.showSaveDialog(null);
file = chooser.getSelectedFile();
try {
if(!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] b = area.getText().getBytes();
fos.write(b);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}else if(item == closeItem) { //关闭
System.exit(1);
}
}
}
}