Java实验三(下):仿记事本

实验目的

能够掌握常用组件的基本操作以及输入输出流的使用

实验内容

1、设计一个仿记事本软件
2、实现新建、打开、保存、另存为等基本功能

实验代码

package experiment_three;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class project3 extends JFrame implements ActionListener{
    JMenu menu;//菜单
    JMenuItem mt1, mt2, mt3, mt4, mt0; //子菜单
    FileDialog save, open; //保存、打开的窗口
    TextArea textArea; //文本域
    FileWriter fw;
    BufferedWriter bw;
    FileReader fr;
    BufferedReader br;
    String oldFile;
    String oldFolder;
    
    public project3() {
        super("记事本实验");
        //菜单栏
        JMenuBar menuBar = new JMenuBar();
        menu = new JMenu("文件",true);
        mt1 = new JMenuItem("新建");
        mt1.addActionListener(this);
        mt2 = new JMenuItem("打开");
        mt2.addActionListener(this);
        mt3 = new JMenuItem("保存");
        mt3.addActionListener(this);
        mt4 = new JMenuItem("另存为");
        mt4.addActionListener(this);
        mt0 = new JMenuItem("退出");
        mt0.addActionListener(this);

        this.setJMenuBar(menuBar);
        menuBar.add(menu);
        menu.add(mt1);
        menu.add(mt2);
        menu.add(mt3);
        menu.add(mt4);
        menu.addSeparator();
        menu.add(mt0);

        //文本区域
        textArea = new TextArea("", 10, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
        save = new FileDialog(this, "保存文件对话框", FileDialog.SAVE);
        open = new FileDialog(this, "打开文件对话框", FileDialog.LOAD);
        this.add(textArea);
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(600, 600);
        this.setLocation(screenSize.width/2-this.getWidth()/2,screenSize.height/2-this.getHeight()/2);//居中显示
        this.setVisible(true);
        //会关闭所有窗口
        //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
    	//新建文件
        if (e.getSource().equals(mt1)) {
        	new project3();
        }
        //打开文件
        if (e.getSource().equals(mt2)) {
            open();
        }
        //保存文件
        if (e.getSource().equals(mt3)) {
            save();
        }
        //文件另存为
        if (e.getSource().equals(mt4)) {
            saveTo();
        }
        //退出
        if (e.getSource().equals(mt0)) {
        	System.exit(0);
        }
    }

    //打开文件对话框
    private void open() {
        String s;
        open.setVisible(true);
        if (open.getFile() != null) {
        	oldFile = open.getFile();
            oldFolder = open.getDirectory();
            File file = new File(open.getDirectory(), open.getFile());
            try {
                fr = new FileReader(file);
                String encoding = fr.getEncoding();
                InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),encoding);
                br = new BufferedReader(inputStreamReader);
                textArea.setText("");
                while ((s = br.readLine())!= null) {
                    textArea.append(s + "\r\n");
                }
                fr.close();
                inputStreamReader.close();
                br.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //文件另存为
    private void saveTo(){
        save.setVisible(true);
        try {
            if (save.getFile() != null) {
                File file = new File(save.getDirectory(),save.getFile());
                fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                bw.write(textArea.getText(), 0, (textArea.getText()).length());
                bw.flush();
                bw.close();
                fw.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //保存文件
    private void save() {
        String oldFile = this.oldFile;
        String oldFolder = this.oldFolder;
        if (oldFile != null && oldFolder != null) {
            File file = new File(oldFolder,oldFile);
            if (file.exists()) {
                try {
                    file.createNewFile();
                    fw = new FileWriter(file);
                    bw = new BufferedWriter(fw);
                    bw.write(textArea.getText(), 0, (textArea.getText()).length());
                    bw.flush();
                    bw.close();
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            saveTo();
        }
    }
  //main
    public static void main(String[] args) {
    	new project3();
    }
}

运行结果

1.腾龙梦屋文章内容无特殊注明皆为源儿原创,转载请注明来源,谢谢!
2.若有相关文章侵犯您的权益,请联系源儿删除,谢谢!
3.相关软件、资料仅供学习参考使用,在24h内务必删除!
腾龙梦屋 » Java实验三(下):仿记事本
加速支持