Java 【笔记本】

一.

*/


import java.awt.*; //普通组件包
import javax.swing.*; //面板包
import java.awt.event.*;  //事件
import java.io.*;  //io流操作

/*
 笔记本实现功能:
   1.保存
*/
public class demo extends JFrame implements ActionListener{
    JMenuBar cd; //菜单条
    JMenu cd1,cd2; //菜单【保存】【】
    JMenuItem cdx2,cdx3; //菜单下的每一项
    JTextArea wby; //文本域

    public static void main(String[] args) {
        demo d = new demo();
    }

    demo(){//构造函数
     cd = new JMenuBar(); //菜单条

     cd1 = new JMenu("文件(F)"); //菜单 【文件】
     cd1.setMnemonic('F');
     cd2 = new JMenu("编辑(E)"); //菜单 【编辑】
     cd2.setMnemonic('E');
     //cdx2
     cdx2 = new JMenuItem("打开",new ImageIcon("1.png"));
     cdx2.addActionListener(this);
     cdx2.setActionCommand("open");
     //cdx3
     cdx3 = new JMenuItem("保存",new ImageIcon("1.png"));
     cdx3.addActionListener(this);
     cdx3.setActionCommand("save");

     wby = new JTextArea();

     cd1.add(cdx2); cd1.add(cdx3);
     cd.add(cd1);  cd.add(cd2);

     this.setJMenuBar(cd);   
     this.add(wby);

      //窗口参数设置
      this.setTitle("记事本"); //窗口的标题
      this.setIconImage((new ImageIcon("1.png").getImage()));
      this.setSize(400,300);   //设置窗口大小
      this.setLocation(300,300);  //设置窗口位置
      this.setVisible(true);  //窗口的可视
      this.setResizable(false);  //禁用调整大小
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("open")){
            //System.out.println("打开");
            JFileChooser wjxz = new JFileChooser();  //文件选择
            wjxz.setDialogTitle("打开"); //打开界面标题
            wjxz.showOpenDialog(null);   //打开对话框
            wjxz.setVisible(true);  //设置可视化

            //打开用户的全路径
            String way = wjxz.getSelectedFile().getAbsolutePath();
            System.out.println(way);
            FileReader wjl = null; //文件流
            BufferedReader hcl =null;

            try{
                wjl = new FileReader(way); //文件流
                hcl = new BufferedReader(wjl);
                String s ="" ,zfc="";
                while((s=hcl.readLine())!=null){
                     zfc+=(s+"
"); //字符串连接
                }
                wby.setText(zfc); //文本域添加内容
            }catch(Exception e0){

            }finally{
                try{
                    wjl.close();
                    hcl.close();
                }catch(Exception e1){}
            }
        }
        else if(e.getActionCommand().equals("save")){
            //System.out.println("保存");
            JFileChooser ljxz = new JFileChooser();  //文件选择
            ljxz.setDialogTitle("打开"); //打开界面标题
            ljxz.showSaveDialog(null);   //保存对话框
            ljxz.setVisible(true);  //设置可视化

            String way_one = ljxz.getSelectedFile().getAbsolutePath();
            try{
                PrintStream p1 = new PrintStream(way_one); //打印流
                System.setOut(p1); //设置输出位置
                System.out.println(this.wby.getText());  //获取文本域的文字
            }
            catch(Exception e2){

            }

        }
    }

}
View Code
原文地址:https://www.cnblogs.com/Crown-V/p/12607441.html