62,614
社区成员
发帖
与我相关
我的任务
分享
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class NoteBook extends JFrame {
private JTextArea ta;
private JMenuBar mb;
private JMenu file;
private JMenuItem open, color, exit;
public NoteBook() {
super("记事本");
Container c = getContentPane();
mb = new JMenuBar();
open = new JMenuItem("打开");
color=new JMenuItem("颜色");
exit = new JMenuItem("退出");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fc=new JFileChooser();
fc.setSize(300,300);
fc.showOpenDialog(NoteBook.this);
}
});
color.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JColorChooser.showDialog(NoteBook.this, "请选择颜色", Color.RED);
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
file=new JMenu("文件");
mb.add(file);
file.add(open);
file.add(color);
file.add(exit);
setJMenuBar(mb);
ta = new JTextArea();
add(ta);
setSize(800, 600);
setVisible(true);
}
public static void main(String args[]) {
NoteBook nb = new NoteBook();
}
}
JFileChooser jfc=new JFileChooser();
int t=jfc.showOpenDialog(frame);
File file=jfc.getSelectedFile();
if(t==JFileChooser.APPROVE_OPTION){
sourceText.setText(file.getAbsolutePath());
}