java模拟实现记事本功能

package com.dh.ch11;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Note extends JFrame implements ActionListener {
	private JMenuBar menuBar;
	private JMenu menuFile, menuHelp;
	private JMenuItem miNew, miOpen, miSave, miExit, miAbout;
	private JScrollPane sp;
	private JTextArea txtContent;
	public Note() {
		super("记事本");
		menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		menuFile = new JMenu("文件");
		menuHelp = new JMenu("帮助");
		menuBar.add(menuFile);
		menuBar.add(menuHelp);
		miNew = new JMenuItem("新建");
		miOpen = new JMenuItem("打开");
		miSave = new JMenuItem("保存");
		miExit = new JMenuItem("退出");
		miAbout = new JMenuItem("关于");
		menuFile.add(miNew);
		menuFile.add(miOpen);
		menuFile.add(miSave);
		menuFile.addSeparator();
		menuFile.add(miExit);
		menuHelp.add(miAbout);
		// 注册监听
		miNew.addActionListener(this);
		miOpen.addActionListener(this);
		miSave.addActionListener(this);
		miExit.addActionListener(this);
		miAbout.addActionListener(this);
		txtContent = new JTextArea(20, 30);
		sp = new JScrollPane(txtContent);
		this.add(sp);
		this.setSize(400, 300);
		this.setLocation(100, 100);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == miNew) {
			// 清空文本域中的内容
			txtContent.setText("");
		}
		if (source == miOpen) {
			// 清空文本域中的内容
			txtContent.setText("");
			// 调用打开文件方法
			openFile();
		}
		if (source == miSave) {
			// 调用保存文件方法
			saveFile();
		}
		if (source == miExit) {
			// 系统退出
			System.exit(0);
		}
		if (source == miAbout) {
			JOptionPane.showMessageDialog(this,"版本:V20141110\n作者:121项目组\n版权:青岛誉金电子科技有限公司",
					"关于",JOptionPane.WARNING_MESSAGE);
		}
	}
	// 打开文件的方法
	private void openFile() {
		// 文件输入流,用于读文件
		FileReader fread = null;
		// 缓冲流
		BufferedReader bread = null;
		// 实例化一个文件对话框对象
		JFileChooser fc = new JFileChooser();
		// 显示文件打开对话框
		int rVal = fc.showOpenDialog(this);
		// 如果单击确定(Yes/OK)
		if (rVal == JFileChooser.APPROVE_OPTION) {
			// 获取文件对话框中用户选中的文件名
			String fileName = fc.getSelectedFile().getName();
			// 获取文件对话框中用户选中的文件所在的路径
			String path = fc.getCurrentDirectory().toString();
			try {
				// 创建一个文件输入流,用于读文件
				fread = new FileReader(path + "/" + fileName);
				// 创建一个缓冲流
				bread = new BufferedReader(fread);
				// 从文件中读一行信息
				String line = bread.readLine();
				// 循环读文件中的内容,并显示到文本域中
				while (line != null) {
					txtContent.append(line + "\n");
					// 读下一行
					line = bread.readLine();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					if(bread != null){
						bread.close();
					}
					if (fread != null) {
						fread.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	// 保存文件的方法
	private void saveFile() {
		//输出流
		FileWriter fwriter = null;
		// 实例化一个文件对话框对象
		JFileChooser fc = new JFileChooser();
		// 显示文件保存对话框
		int rVal = fc.showSaveDialog(this);
		// 如果单击确定(Yes/OK)
		if (rVal == JFileChooser.APPROVE_OPTION) {
			// 获取文件对话框中用户选中的文件名
			String fileName = fc.getSelectedFile().getName();
			// 获取文件对话框中用户选中的文件所在的路径
			String path = fc.getCurrentDirectory().toString();
			try {
				// 创建一个文件输出流,用于写文件
				fwriter = new FileWriter(path + "/" + fileName);
				// 将文本域中的信息写入文件中
				fwriter.write(txtContent.getText());
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					if (fwriter != null) {
						fwriter.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) {
		Note f = new Note();
		f.setVisible(true);
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值