JAVA--简单记事本

来源:互联网 发布:大陆单片机 编辑:程序博客网 时间:2024/05/19 04:03

代码分为三部分:界面、打开、保存

界面代码:
这里写图片描述

public void init() {        //外框        frame = new Frame("记事本");        frame.setVisible(true);        frame.setSize(1300, 900);        frame.setLocation(300, 400);        frame.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });        //        MenuBar mb = new MenuBar();        Menu file = new Menu("文件");        help = new Menu("帮助");        MenuItem shelp = new MenuItem("帮助");        open = new MenuItem("打开");        save = new MenuItem("保存");        file.add(open);        file.add(save);        help.add(shelp);        ta = new TextArea();        ta.setFont(new Font("黑体",0,30));        frame.add(ta);        mb.add(file);        mb.add(help);        frame.setMenuBar(mb);    }

打开文件:

    public void loadFile() throws Exception  {        fd_load.setVisible(true);        String setFile = fd_load.getDirectory() + fd_load.getFile();        FileInputStream fis = new FileInputStream(setFile);        byte b[] = new byte[1024];        int n;        while((n = fis.read(b)) != -1) {            String s = new String(b, 0, n);            ta.append(s);        }    }

保存文件:

public void saveFile() throws Exception {        fd_save.setVisible(true);        String path = fd_save.getDirectory()+fd_save.getFile() +".txt";        FileOutputStream fos = new FileOutputStream(path);        String sa = ta.getText();        byte b[] = sa.getBytes();        fos.write(b);        fos.close();    }

完整源代码

package test;import java.awt.*;import java.awt.event.*;import java.io.FileInputStream;import java.io.FileOutputStream;import javax.swing.JOptionPane;public class txt implements ActionListener{    private Frame frame;    private Menu help;    private MenuItem open;    private MenuItem save;    private TextArea ta;    private FileDialog fd_load;    private FileDialog fd_save;    private Frame fhelp;    public static void main(String[] args) {        txt hj = new txt();        hj.init();    }    public void init() {        //外框        frame = new Frame("记事本");        frame.setVisible(true);        frame.setSize(1300, 900);        frame.setLocation(300, 400);        frame.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });        //        MenuBar mb = new MenuBar();        Menu file = new Menu("文件");        help = new Menu("帮助");        MenuItem shelp = new MenuItem("帮助");        open = new MenuItem("打开");        save = new MenuItem("保存");        help.addActionListener(this);        open.addActionListener(this);        save.addActionListener(this);        file.add(open);        file.add(save);        help.add(shelp);        ta = new TextArea();        ta.setFont(new Font("黑体",0,30));        frame.add(ta);        mb.add(file);        mb.add(help);        frame.setMenuBar(mb);        fd_load = new FileDialog(frame, "打开文件", FileDialog.LOAD);        fd_save = new FileDialog(frame, "保存文件", FileDialog.SAVE);    }    public void actionPerformed(ActionEvent e)  {        String s = e.getActionCommand();        try{        if(s.equals("打开")) {            this.loadFile();        } else if(s.equals("保存")) {                this.saveFile();                } else if(s.equals("帮助")) {                 this.help();            }        }catch (Exception e1) {                e1.printStackTrace();            }    }    public void saveFile() throws Exception {        fd_save.setVisible(true);        String path = fd_save.getDirectory()+fd_save.getFile() +".txt";        FileOutputStream fos = new FileOutputStream(path);        String sa = ta.getText();        byte b[] = sa.getBytes();        fos.write(b);        fos.close();    }    public void loadFile() throws Exception  {        fd_load.setVisible(true);        String setFile = fd_load.getDirectory() + fd_load.getFile();        FileInputStream fis = new FileInputStream(setFile);        byte b[] = new byte[1024];        int n;        while((n = fis.read(b)) != -1) {            String s = new String(b, 0, n);            ta.append(s);        }    }    public void help() {        JOptionPane.showMessageDialog(null, "@huangju", "JAVA--记事本", 2);    }}
0 0