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);
}
}
java模拟实现记事本功能
最新推荐文章于 2021-03-22 19:01:14 发布