标签:关闭 stack ble except npe eof instance list 部分
package Lemon;
import java.awt.*;
import javax.swing.*;
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 java.util.EventObject;
public class Note implements ActionListener{
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu,formatMenu,viewMenu,helpMenu;
JMenuItem newItem,openItem,saveItem,closeItem,exitItem;
ImageIcon newIcon,openIcon,saveIcon,closeIcon;
JScrollPane scroll;
JTextArea area;
JFileChooser chooser;
File file;
private JMenuItem item;
public Note(){
frame = new JFrame("记事本");
bar = new JMenuBar();
fileMenu = new JMenu("文件");
editMenu = new JMenu("编辑");
formatMenu = new JMenu("格式");
viewMenu=new JMenu("查找");
helpMenu=new JMenu("帮助");
area =new JTextArea();
scroll = new JScrollPane(area);
newItem = new JMenuItem("新建(N)",newIcon);
openItem = new JMenuItem("打开(O)",openIcon);
saveItem = new JMenuItem("另存为(s)",saveIcon);
closeItem = new JMenuItem("关闭(C)",closeIcon);
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
newItem.setMnemonic(KeyEvent.VK_N);
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,java.awt.Event.CTRL_MASK));
openItem.setMnemonic(KeyEvent.VK_O);
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,java.awt.Event.CTRL_MASK));
saveItem.setMnemonic(KeyEvent.VK_S);
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,java.awt.Event.CTRL_MASK));
closeItem.setMnemonic(KeyEvent.VK_C);
closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,java.awt.Event.CTRL_MASK));
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(viewMenu);
bar.add(helpMenu);
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setSize(800,500);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if(obj instanceof JMenuItem){
JMenuItem item = (JMenuItem) obj;
if(item == newItem){
new Note();
}
}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(IOException e){
e.printStackTrace();
}
}else if(item == closeItem){
System.exit(1);
}
}
package Lemon;
public class Test {
public static void main(String[] args) {
Note note = new Note();
}
}
标签:关闭 stack ble except npe eof instance list 部分
原文地址:https://www.cnblogs.com/buxiu888/p/11918065.html