文本编译器
package caidan;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.*;
public class JMenuDemo1 implements ActionListener {
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu;
JMenuItem newItem,openItem,saveItem,closeItem;
ImageIcon newIcon,openIcon,saveIcon,closeIcon;
JScrollPane scorll;
JTextArea area;
File file;
private JFileChooser chooser;
public JMenuDemo1(){
frame = new JFrame("LXZ的小本本");
bar = new JMenuBar();
fileMenu = new JMenu("文件");
editMenu = new JMenu("编辑");
newItem = new JMenuItem("新建(N)",newIcon);
openItem = new JMenuItem("打开(O)",openIcon);
saveItem = new JMenuItem("另存为(A)",saveIcon);
closeItem = new JMenuItem("关闭(X)",closeIcon);
area = new JTextArea();
scorll = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
newIcon = new ImageIcon("D:"+File.separator+"images"+File.separator+"3.png");
openIcon = new ImageIcon("D:"+File.separator+"images"+File.separator+"1.png");
saveIcon = new ImageIcon("D:"+File.separator+"images"+File.separator+"2.png");
closeIcon = new ImageIcon("D:"+File.separator+"images"+File.separator+"4.png");
area = new JTextArea();
scorll = new JScrollPane(area);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_A);
closeItem.setMnemonic(KeyEvent.VK_X);
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_DOWN_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_DOWN_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_DOWN_MASK));
closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
bar.add(fileMenu);
bar.add(editMenu);
frame.setJMenuBar(bar);
frame.add(scorll);
frame.setSize(400, 300);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj instanceof JMenuItem) {
JMenuItem item = (JMenuItem) obj;
if (item == newItem) {
new JMenuDemo1();
}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 e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}else if(item == saveItem) {
if(file!=null) {
try {
PrintStream out = new PrintStream(new FileOutputStream(file));
out.print(this.area.getText());
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}else if(item == saveItem) {
chooser.showSaveDialog(null);
file = chooser.getSelectedFile();
if(!file.exists()) {
try {
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fos=new FileOutputStream(file);
byte[] b = area.getText().getBytes();
fos.write(b);
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}else if(item == closeItem) {
System.exit(1);
}
}
}
}
Main方法
package caidan;
public class JMenuDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
new JMenuDemo1();
}
}
截图: