Java记事本

package notebook;
import javax.swing.*;

import java.io.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;

public class NotePad extends JFrame implements ActionListener {

    JTextArea jta=null;
    JMenuBar jmb=null;
    JMenu jm1=null;
    JMenuItem jmi1=null;
    JMenuItem jmi2=null;
    
    public NotePad()
    {
        jta=new JTextArea();
        jmb=new JMenuBar();
        jm1=new JMenu("文件");
        //设置助记符
        jm1.setMnemonic('F');
        this.add(jta);
        jmi1=new JMenuItem("打开",new ImageIcon("file.png"));
        jmi1.addActionListener(this);
        jmi1.setActionCommand("open");
        
        jmi2=new JMenuItem("保存",new ImageIcon("save.png"));
        jmi2.addActionListener(this);
        jmi2.setActionCommand("save");
        
        
        this.setJMenuBar(jmb);
        jmb.add(jm1);
        jm1.add(jmi1);
        jm1.add(jmi2);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400,300);
        setVisible(true);
    }
    public static void main(String[] args) {
        new NotePad();

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("open"))
        {
            JFileChooser jfc1=new JFileChooser();
            jfc1.setDialogTitle("请选择文件");
            jfc1.showOpenDialog(null);
            jfc1.setVisible(true);
            String filename=jfc1.getSelectedFile().getAbsolutePath();
            FileReader fr=null;
            BufferedReader br=null;
            try
            {
                fr=new FileReader(filename);
                br=new BufferedReader(fr);
                String s="";
                String allCon="";
                while((s=br.readLine())!=null)
                {
                    allCon+=s+"\r\n";
                }
                jta.setText(allCon);
            }
            catch (Exception e2)
            {
                // TODO: handle exception
            }
            finally
            {
                try
                {
                    br.close();
                    fr.close();
                }
                catch (Exception e3)
                {
                    
                }
            }
        }
        if(e.getActionCommand().equals("save"))
        {
            JFileChooser jfc=new JFileChooser();
            jfc.setDialogTitle("另存为");
            jfc.showSaveDialog(null);
            jfc.setVisible(true);
            
            String file=jfc.getSelectedFile().getAbsolutePath();
            FileWriter fw=null;
            BufferedWriter bw=null;
            try
            {
                fw=new FileWriter(file);
                bw=new BufferedWriter(fw);
                bw.write(this.jta.getText());
            }
            catch (Exception e2)
            {
                // TODO: handle exception
            }
            finally
            {
                try {
                    bw.close();
                    fw.close();
                } catch (Exception e3) {
                    // TODO: handle exception
                }
            }
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值