package notepad;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Notepad extends JFrame implements ActionListener{
JMenuBar menu=null;
JMenu wenjian,bianji;
JMenuItem a1,a2,a3,a4,b1,b2,b3,b4;
JTextArea wbk=null;
public static void main(String[] args) {
Notepad pad=new Notepad();
}
public Notepad(){
menu=new JMenuBar();
wenjian=new JMenu("文件");
a1=new JMenuItem("打开");
a2=new JMenuItem("保存");
a3=new JMenuItem("另存为");
a4=new JMenuItem("新建");
bianji=new JMenu("编辑");
b1=new JMenuItem("剪切");
b2=new JMenuItem("复制");
b3=new JMenuItem("粘贴");
b4=new JMenuItem("清空");
wbk=new JTextArea();
wenjian.add(a4);
wenjian.add(a1);
wenjian.add(a2);
wenjian.add(a3);
bianji.add(b1);
bianji.add(b2);
bianji.add(b3);
bianji.add(b4);
menu.add(wenjian);
menu.add(bianji);
this.setVisible(true);
this.setSize(500, 600);
this.setLocation(400, 60);
this.setTitle("记事本");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(menu);
this.add(wbk);
a1.addActionListener(this);
a1.setActionCommand("a1");
a2.addActionListener(this);
a2.setActionCommand("a2");
a3.addActionListener(this);
a3.setActionCommand("a3");
a4.addActionListener(this);
a4.setActionCommand("a4");
b3.addActionListener(this);
b3.setActionCommand("b3");
b4.addActionListener(this);
b4.setActionCommand("b4");
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("a1"))
{
JFileChooser wjxz=new JFileChooser();
wjxz.setDialogTitle("选择文件");
wjxz.showOpenDialog(null);
wjxz.setVisible(true);
String lujing=wjxz.getSelectedFile().getAbsolutePath();
FileReader read=null;
BufferedReader read1=null;
try
{
read=new FileReader(lujing);
read1=new BufferedReader(read);
String s="",d="";
while((s=read1.readLine())!=null)
d+=(s+"\n");
wbk.setText(d);
}
catch(Exception e1){}
finally
{
try{
read.close();
read1.close();
}
catch(Exception e2){}
}
}
else if(e.getActionCommand().equals("a3"))
{
JFileChooser wjbc=new JFileChooser();
wjbc.setDialogTitle("另存为");
wjbc.showSaveDialog(null);
wjbc.setVisible(true);
String lujing2=wjbc.getSelectedFile().getAbsolutePath();
PrintStream p1=null;
try
{
p1=new PrintStream(lujing2);
System.setOut(p1);
System.out.println(this.wbk.getText());
}
catch(Exception e3){}
finally
{
try
{
p1.close();
}
catch(Exception e4){}
}
}
else if(e.getActionCommand().equals("a4"))
{
if(this.wbk.getText().equals(""))
{
this.wbk.setText(null);
}
else
{
JOptionPane jo;
jo = new JOptionPane();
jo.showMessageDialog(null,"提示:"+"\n"+"请先保存文件再清空!");
}
}
else if(e.getActionCommand().equals("a2"))
{
String lujing="C:\\Users\\kangbiao\\Desktop";
PrintStream p1=null;
try
{
p1=new PrintStream(lujing);
System.setOut(p1);
System.out.println(this.wbk.getText());
}
catch(Exception e3){}
finally
{
try
{
p1.close();
}
catch(Exception e4){}
}
}
else if(e.getActionCommand().equals("b4"))
{
this.wbk.setText(null);
}
}
}
java记事本
最新推荐文章于 2023-02-21 19:31:42 发布