该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class B8_8 extends JFrame implements ActionListener{
JMenuBar menu;
JMenu menu1,menu2;
JMenuItem menuSe2,menuSe3;
JTextArea textArea;
public static void main(String[] args) {
B8_8 bb=new B8_8();
}
public B8_8(){
menu=new JMenuBar();
menu1=new JMenu("文件(F)");
menu1.setMnemonic('F');//设置当前模型上的键盘助记符
menu2=new JMenu("编辑(E)");
menu2.setMnemonic('E');
menuSe2=new JMenuItem("打开",new ImageIcon("E:/1.jpg"));
menuSe2.addActionListener(this);
menuSe2.setActionCommand("open");
menuSe3=new JMenuItem("保存",new ImageIcon("E:/2.jpg"));
menuSe3.addActionListener(this);
menuSe3.setActionCommand("save");
textArea=new JTextArea();
menu1.add(menuSe2);menu1.add(menuSe3);
menu.add(menu1);menu.add(menu2);
this.setJMenuBar(menu);this.add(textArea);
ImageIcon tp1=new ImageIcon("E:/壁纸/1.jpg");
this.setIconImage(tp1.getImage());
this.setTitle("记事本");
//mb=new Mymb6();
//this.add(mb);
//this.addWindowListener(mb);
this.setSize(400,300);
this.setLocation(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("open")){
//System.out.println("打开");
JFileChooser fileChoose=new JFileChooser();
fileChoose.setDialogTitle("文件打开");
fileChoose.showOpenDialog(null);
fileChoose.setVisible(true);
String files=fileChoose.getSelectedFile().getAbsolutePath();
//得到用户选择的全路径
FileReader reader=null;
BufferedReader buffer=null;
try{
reader=new FileReader(files);
buffer=new BufferedReader(reader);
String s="",zfc="";
while((s=buffer.readLine())!=null)
{
zfc+=(s+"\n");
}
}catch(Exception aa){
}
finally {
try{
reader.close();
buffer.close();
}catch(Exception ee){
}
}
}else if(e.getActionCommand().equals("save")){
System.out.println("保存");
JFileChooser filechoose=new JFileChooser();
filechoose.setDialogTitle("另存为");
filechoose.showSaveDialog(null);
filechoose.setVisible(true);
String str=filechoose.getSelectedFile().getAbsolutePath();
try{
PrintStream print =new PrintStream(str);
System.setOut(print);
System.out.println(this.textArea.getText());
}catch(Exception aa){
}
}
}
}