import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;**
public class jishiben extends JFrame implements ActionListener {
JMenuBar cd;
JMenu cd1, cd2;
JMenuItem cdx1, cdx2, cdx3;
JTextArea wby;
JScrollPane gd;
public static void main(String[] args) {
new jishiben();
}
jishiben() {
cdx3 = new JMenuItem("退出");
cd = new JMenuBar();
cd1 = new JMenu("文件");
cd2 = new JMenu("编辑");
cdx1 = new JMenuItem("打开");
cdx2 = new JMenuItem("保存");
cdx1.addActionListener(this);
cdx2.addActionListener(this);
cdx3.addActionListener(this);
cdx1.setActionCommand("open");
cdx2.setActionCommand("save");
cdx3.setActionCommand("exit");
wby = new JTextArea();
gd = new JScrollPane(wby);
cd1.add(cdx1);
cd1.add(cdx2);
cd1.add(cdx3);
cd.add(cd1);
cd.add(cd2);
this.setJMenuBar(cd);
this.add(gd);
this.setSize(200, 200);
this.setTitle("汤预的记事本");
this.setLocation(400, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("open")) {
JFileChooser windows = new JFileChooser();
windows.setDialogTitle("文件打开");
windows.showOpenDialog(null);
windows.setVisible(true);
String ty = windows.getSelectedFile().getAbsolutePath();// 得到所选文件的全路径
FileReader a = null;
BufferedReader b = null;
try {
a = new FileReader(ty);
b = new BufferedReader(a);
char[] c = new char[100000];
int len = 0;
while ((len = b.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
b.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
a.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (e.getActionCommand().equals("save")) {
JFileChooser baocun = new JFileChooser();
baocun.setDialogTitle("另存为");
baocun.showSaveDialog(null);
baocun.setVisible(true);
try {
String ty1 = baocun.getSelectedFile().getAbsolutePath();// 得到所选文件的全路径
PrintStream p = new PrintStream(ty1);
System.setOut(p);
System.out.println(this.wby.getText());
} catch (Exception ee) {
// TODO: handle exception
}
if (e.getActionCommand().equals("exit")) {
System.exit(0);
}
}
}
}
用java编写一个记事本
最新推荐文章于 2023-07-02 15:58:48 发布