返回顶部

收藏

简易的记事本

更多
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Notebook extends JFrame{
    JTextArea jta=null;
    JScrollPane jsp=null;
    JMenuBar jbr=null;
    JMenu jm1,jm2;
    JMenuItem jmi2,jmi3,jmi4;
    public static void main(String[] args) {
        Notebook nb=new Notebook();
    }

    public Notebook()
    {
        jta=new JTextArea();
        jsp=new JScrollPane(jta);

        jmi2=new JMenuItem("打开");
        jmi2.setActionCommand("打开");
        jmi2.addActionListener(new Mylistener());

        jmi3=new JMenuItem("保存");
        jmi3.setActionCommand("保存");
        jmi3.addActionListener(new Mylistener());

        jmi4=new JMenuItem("查看帮助");
        jmi4.setActionCommand("查看帮助");
        jmi4.addActionListener(new Mylistener());

        jm1=new JMenu("选项");
        jm1.add(jmi2);
        jm1.add(jmi3);
        jm2=new JMenu("帮助");
        jm2.add(jmi4);
        jbr=new JMenuBar();
        jbr.add(jm1);
        jbr.add(jm2);
        this.add(jsp);
        this.add(jbr,BorderLayout.NORTH);
        this.setTitle("记事本");
        this.setSize(600,500);
        this.setLocation(200,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    class Mylistener implements ActionListener
    {
        InputStreamReader isr=null;
        BufferedReader bfd=null;
        BufferedWriter bfw=null;
        JFileChooser jfc=null;
        FileReader fd=null;
        File creatfile=null;
        FileWriter fw=null;

        public void actionPerformed(ActionEvent arg0) {

                if(arg0.getActionCommand().equals("打开")){
                jfc=new JFileChooser();
                jfc.setDialogTitle("请选择要打开的文件");
                jfc.showOpenDialog(null);
                jfc.setVisible(true);
                String filename=jfc.getSelectedFile().getAbsolutePath();

                try {
                    fd=new FileReader(filename);
                    bfd=new BufferedReader(fd);

                    String s="",allstring="";
                    while((s=bfd.readLine())!=null){
                        allstring+=s+"\\n";
                    }
                    jta.setText(allstring);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    try {
                        fd.close();
                        bfd.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }else if(arg0.getActionCommand().equals("保存")){
                //很重要的一个组件JFileChooser
                jfc=new JFileChooser();
                jfc.setDialogTitle("请选择要保存的路径");
                jfc.showSaveDialog(null);
                jfc.setVisible(true);
                String savepath=jfc.getSelectedFile().getAbsolutePath();

                try {
                    fw=new FileWriter(savepath);
                    bfw=new BufferedWriter(fw);

                    String newfile=jta.getText();
                    bfw.write(newfile);

                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    try {
                        bfw.close();
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }       }

    }
}
//该片段来自于http://outofmemory.cn

标签:java,图形

收藏

0人收藏

支持

0

反对

0

发表评论