用java开发的一个简单的记事本程序

代码如下:

package IO;
import java.io.*;
import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
public class notepad extends JFrame implements ActionListener{

    /**
     * @param args
     */
    JTextArea jta=null;
//  定义菜单条
    JMenuBar jmb=null;
    JMenu jm1=null;
    JMenuItem jmi1=null;
    JMenuItem jmi2=null;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        notepad np=new notepad();
    }
    public notepad(){
        jta=new JTextArea();
        jmb=new JMenuBar();
        jm1=new JMenu("文件");
//      设置助记符
        jm1.setMnemonic('F');
        jmi1=new JMenuItem("打开",new ImageIcon("2.png"));
        jmi1.addActionListener(this);
        jmi1.setActionCommand("open");
        jmi2=new JMenuItem("保存",new ImageIcon("3.png"));
        jmi2.addActionListener(this);
        jmi2.setActionCommand("save");
        this.setJMenuBar(jmb);
        jmb.add(jm1);
        jm1.add(jmi1);
        jm1.add(jmi2);
        this.add(jta);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400,300);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
//      判断是哪一个操蛋被选中
        if(e.getActionCommand().equals("open")){
//          System.out.print("打开");
            JFileChooser jfc1=new JFileChooser();
            jfc1.setDialogTitle("请选择文件");
            jfc1.showOpenDialog(null);
//          显示
            jfc1.setVisible(true);
//          得到选择的路径
            String filename=jfc1.getSelectedFile().getAbsolutePath();
            System.out.print(filename);
            FileReader fr=null;
            BufferedReader br=null;
            try {
                fr=new FileReader(filename);
                br=new BufferedReader(fr);

//              显示打印信息
                String s="";
                String allCon="";
                while((s=br.readLine())!=null){
//                  bw.write(s+"\r\n");
                    allCon+=s+"\r\n";
                }
//              繁重到
                jta.setText(allCon);
            } catch (Exception e2) {
                // TODO: handle exception
            }finally{
                try {
                    br.close();
                    fr.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }else 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 {
                    fw.close();
                    bw.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        }
    }

}
java写的记事本,1000行代码,基本上所有的功能都全了(和微软系统自带的记事本的相似度>70%) 其中菜单里新建模块的代码如下: // 菜单 // 新建(N)按钮事件监听 newTextItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (selectedFile == null && textArea.getText().equals("")) { return; } else { int btn = JOptionPane.showConfirmDialog(container, "是否保存到" + selectedFile + "?", "是否保存", JOptionPane.YES_NO_CANCEL_OPTION); if (btn == JOptionPane.CANCEL_OPTION) { return; } else if (btn == JOptionPane.YES_OPTION) { if (selectedFile == null && !textArea.getText().equals("")) { choose = new JFileChooser(); int state = choose.showSaveDialog(container); if (state == JFileChooser.APPROVE_OPTION) { try { File file = choose.getSelectedFile(); BufferedWriter bw = new BufferedWriter( new FileWriter(file)); String str = textArea.getText(); String[] lines = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.flush(); file.createNewFile(); bw.close(); } catch (IOException e) { JOptionPane.showConfirmDialog(container, "保存文件失败!", "ERROR", JOptionPane.ERROR_MESSAGE); } } } else if (selectedFile != null) { try { BufferedWriter bw = new BufferedWriter( new FileWriter(selectedFile)); String str = textArea.getText(); String[] lines = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.flush(); bw.close(); } catch (IOException e) { // JOptionPane.showConfirmDialog(container, // "保存文件失败!", // "ERROR", JOptionPane.ERROR_MESSAGE); } } } } textArea.setText(""); newPage = true; selectedFile = null; textField.setText(""); } }); 如果你初学或也在写记事本,这个绝对对你有帮助,且最适合你1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值