代码如下
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.Vector;
import javax.swing.border.*;public class NoteBook extends JFrame{
public JTextArea ta;
private JMenuBar mb;
private JMenu file,edit,form,help;
private JMenuItem newfile,open,save,exit;
private JMenuItem cut,copy,paste,select_all;
private JMenuItem font,about;
private JScrollPane jsp;
private JFileChooser jfc;
public JButton bt1,bt2;
public JDialog jDialog = null;
public NoteBook(){
super("记事本");
Container c = getContentPane();
//bt1 = new JButton("确定");
//bt2 = new JButton("取消");
jfc = new JFileChooser();
mb = new JMenuBar();
file = new JMenu("文件");
edit = new JMenu("编辑");
form = new JMenu("格式");
help = new JMenu("帮助");
newfile = new JMenuItem("新建");
open = new JMenuItem("打开");
save = new JMenuItem("保存");
exit = new JMenuItem("退出");
cut = new JMenuItem("剪切");
copy = new JMenuItem("复制");
paste = new JMenuItem("粘贴");
select_all = new JMenuItem("全选");
font = new JMenuItem("字体...");
about = new JMenuItem("关于...");
newfile.addActionListener(new Handler1());
open.addActionListener(new Handler1());
save.addActionListener(new Handler1());
exit.addActionListener(new Handler1());
cut.addActionListener(new Handler1());
copy.addActionListener(new Handler1());
paste.addActionListener(new Handler1());
select_all.addActionListener(new Handler1());
font.addActionListener(new Handler1());
about.addActionListener(new Handler1());
mb.add(file);
mb.add(edit);
mb.add(form);
mb.add(help);
file.add(newfile);
file.add(open);
file.add(save);
file.add(exit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(select_all);
form.add(font);
help.add(about);
setJMenuBar(mb);
ta = new JTextArea();
//ta.setColumns(20);
ta.setLineWrap(true);
add(ta);
jsp = new JScrollPane(ta);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(jsp);
setSize(800,600);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
} //打开文件
private void openfile(){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
try{
ta.read(new FileReader(file),null);
}
catch(IOException exp) {}
}
}
//保存文件
private void savefile(){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
try{
ta.write(new FileWriter(file));
}
catch(IOException exp) {}
}
}
//新建
private void new_file(){
ta.setText("");
}
//剪切
private void cut_file(){
ta.cut();
}
//复制
private void copy_file(){
ta.copy();
}
//粘贴
private void paste_file(){
ta.paste();
}
private void select_file(){
ta.selectAll();
}
//class font_mode{
public void font_mode(){
//第一个标签
JFrame f = new JFrame();
JDialog jDialog = null;
jDialog = new JDialog(f,"字体",true);
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
Container dp = jDialog.getContentPane();
dp.setLayout(gbl);
//JButton bt1,bt2;
bt1 = new JButton("确定");
bt2 = new JButton("取消");
/* JPanel pane = new JPanel();
pane.setLayout(new GridLayout(4,4)); //一行两列
pane.add(bt1);
pane.add(bt2); */
bt1.addActionListener(new Handler2());
bt2.addActionListener(new Handler2());
//确定按钮
gbc.gridx = 3; //第3列
gbc.gridy = 5; //第5行
gbc.gridwidth = 1; //占1单位宽度
gbc.gridheight = 1; //占1单位高度
gbc.weightx = 0;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(20,15,15,15);
gbc.ipadx = 0;
gbc.ipady = 0;
//bt1.setSize(50,40);
gbl.setConstraints(bt1,gbc);
dp.add(bt1);
//取消按钮
gbc.gridx = 4; //第3列
gbc.gridy = 5; //第5行
gbc.gridwidth = 1; //占1单位宽度
gbc.gridheight = 1; //占1单位高度
gbc.weightx = 0;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(20,15,15,15);
gbc.ipadx = 0;
gbc.ipady = 0;
gbl.setConstraints(bt2,gbc);
dp.add(bt2);
jDialog.setBounds(300,100,600,500);
jDialog.setVisible(true);
//jDialog.show();
}
//}
//主函数
public static void main(String args[]){
NoteBook nb = new NoteBook();
}
class Handler1 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == open) //打开
openfile();
if(e.getSource() == save) //保存
savefile();
if(e.getSource() == newfile) //新建
new_file();
if(e.getSource() == exit) //退出
System.exit(0);
if(e.getSource() == cut)
cut_file();
if(e.getSource() == copy)
copy_file();
if(e.getSource() == paste)
paste_file();
if(e.getSource() == select_all)
select_file();
if(e.getSource() == font){
//font_mode fm = new font_mode(this);
font_mode();
}
if(e.getSource() == about){
JOptionPane.showMessageDialog(null,"A06计算机1班" + "\n" + "江挺" + "\n" + "060505107");
}
if(e.getSource() == bt1){
}
if(e.getSource() == bt2){
jDialog.dispose();
}
}
}
class Handler2 implements ActionListener{
public void actionPerformed(ActionEvent ee){
if(ee.getSource() == bt1){}
if(ee.getSource() == bt2){
jDialog.dispose();
}
}
}
} //最后的括号
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.Vector;
import javax.swing.border.*;public class NoteBook extends JFrame{
public JTextArea ta;
private JMenuBar mb;
private JMenu file,edit,form,help;
private JMenuItem newfile,open,save,exit;
private JMenuItem cut,copy,paste,select_all;
private JMenuItem font,about;
private JScrollPane jsp;
private JFileChooser jfc;
public JButton bt1,bt2;
public JDialog jDialog = null;
public NoteBook(){
super("记事本");
Container c = getContentPane();
//bt1 = new JButton("确定");
//bt2 = new JButton("取消");
jfc = new JFileChooser();
mb = new JMenuBar();
file = new JMenu("文件");
edit = new JMenu("编辑");
form = new JMenu("格式");
help = new JMenu("帮助");
newfile = new JMenuItem("新建");
open = new JMenuItem("打开");
save = new JMenuItem("保存");
exit = new JMenuItem("退出");
cut = new JMenuItem("剪切");
copy = new JMenuItem("复制");
paste = new JMenuItem("粘贴");
select_all = new JMenuItem("全选");
font = new JMenuItem("字体...");
about = new JMenuItem("关于...");
newfile.addActionListener(new Handler1());
open.addActionListener(new Handler1());
save.addActionListener(new Handler1());
exit.addActionListener(new Handler1());
cut.addActionListener(new Handler1());
copy.addActionListener(new Handler1());
paste.addActionListener(new Handler1());
select_all.addActionListener(new Handler1());
font.addActionListener(new Handler1());
about.addActionListener(new Handler1());
mb.add(file);
mb.add(edit);
mb.add(form);
mb.add(help);
file.add(newfile);
file.add(open);
file.add(save);
file.add(exit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(select_all);
form.add(font);
help.add(about);
setJMenuBar(mb);
ta = new JTextArea();
//ta.setColumns(20);
ta.setLineWrap(true);
add(ta);
jsp = new JScrollPane(ta);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(jsp);
setSize(800,600);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
} //打开文件
private void openfile(){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
try{
ta.read(new FileReader(file),null);
}
catch(IOException exp) {}
}
}
//保存文件
private void savefile(){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
try{
ta.write(new FileWriter(file));
}
catch(IOException exp) {}
}
}
//新建
private void new_file(){
ta.setText("");
}
//剪切
private void cut_file(){
ta.cut();
}
//复制
private void copy_file(){
ta.copy();
}
//粘贴
private void paste_file(){
ta.paste();
}
private void select_file(){
ta.selectAll();
}
//class font_mode{
public void font_mode(){
//第一个标签
JFrame f = new JFrame();
JDialog jDialog = null;
jDialog = new JDialog(f,"字体",true);
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
Container dp = jDialog.getContentPane();
dp.setLayout(gbl);
//JButton bt1,bt2;
bt1 = new JButton("确定");
bt2 = new JButton("取消");
/* JPanel pane = new JPanel();
pane.setLayout(new GridLayout(4,4)); //一行两列
pane.add(bt1);
pane.add(bt2); */
bt1.addActionListener(new Handler2());
bt2.addActionListener(new Handler2());
//确定按钮
gbc.gridx = 3; //第3列
gbc.gridy = 5; //第5行
gbc.gridwidth = 1; //占1单位宽度
gbc.gridheight = 1; //占1单位高度
gbc.weightx = 0;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(20,15,15,15);
gbc.ipadx = 0;
gbc.ipady = 0;
//bt1.setSize(50,40);
gbl.setConstraints(bt1,gbc);
dp.add(bt1);
//取消按钮
gbc.gridx = 4; //第3列
gbc.gridy = 5; //第5行
gbc.gridwidth = 1; //占1单位宽度
gbc.gridheight = 1; //占1单位高度
gbc.weightx = 0;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(20,15,15,15);
gbc.ipadx = 0;
gbc.ipady = 0;
gbl.setConstraints(bt2,gbc);
dp.add(bt2);
jDialog.setBounds(300,100,600,500);
jDialog.setVisible(true);
//jDialog.show();
}
//}
//主函数
public static void main(String args[]){
NoteBook nb = new NoteBook();
}
class Handler1 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == open) //打开
openfile();
if(e.getSource() == save) //保存
savefile();
if(e.getSource() == newfile) //新建
new_file();
if(e.getSource() == exit) //退出
System.exit(0);
if(e.getSource() == cut)
cut_file();
if(e.getSource() == copy)
copy_file();
if(e.getSource() == paste)
paste_file();
if(e.getSource() == select_all)
select_file();
if(e.getSource() == font){
//font_mode fm = new font_mode(this);
font_mode();
}
if(e.getSource() == about){
JOptionPane.showMessageDialog(null,"A06计算机1班" + "\n" + "江挺" + "\n" + "060505107");
}
if(e.getSource() == bt1){
}
if(e.getSource() == bt2){
jDialog.dispose();
}
}
}
class Handler2 implements ActionListener{
public void actionPerformed(ActionEvent ee){
if(ee.getSource() == bt1){}
if(ee.getSource() == bt2){
jDialog.dispose();
}
}
}
} //最后的括号
改为:
this.dispose();this代表当前的JFrame