java swing设计简易文本编译器
package java_Test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Java_File extends JFrame implements ActionListener {
Container con;
JMenu menu;
JMenuBar jb;
JMenuItem item1,item2,item3;
public static void main(String[] args) {
Java_File frame = new Java_File();
frame.setVisible(true);
}
public Java_File() {
setBounds(100, 100, 400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb = new JMenuBar();
menu = new JMenu("文件");
item1 = new JMenuItem("打开");
item2 = new JMenuItem("保存");
item3 = new JMenuItem("退出");
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
menu.add(item1);
menu.add(item2);
menu.add(item3);
jb.add(menu);
setJMenuBar(jb);
con = getContentPane();
con .setLayout(new FlowLayout());
//button1 = new JButton("打开文件");
//button2 = new JButton("保存文件");
//button1.addActionListener(this);
//button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
JFileChooser f = new JFileChooser(); //创建一个指向用户默认目录的文本对话框
f.showOpenDialog(this); //showOpenDialog(Component parent) 显示文件打开对话框,参数是父组件对象
if(e.getSource() == item1) { //获取触发对象
if(JFileChooser.APPROVE_OPTION == f.showOpenDialog(this))
JOptionPane.showConfirmDialog(this, "你确定要打开吗?");
}
if(e.getSource() == item2) {
f.showSaveDialog(this); //showSaveDialog(Component parent)显示文件保存对话框,参数是父组件对象
}
}
}