实验课老师要求写的,学习了网上n多大佬的博客后,自己写了一个简单的记事本程序
效果图:
实现代码:
package test;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Notepad extends JFrame implements ActionListener {
private static final long serialVersionUID = -6200727203246728269L;
JTextArea jTextArea;
// 文本框组件
JScrollPane jScrollPane;
// 滚动条组件
JToolBar jToolBar;
// 工具条组件
JMenuBar jMenuBar;
// 菜单栏组件
JMenu jMenu;
// 菜单组件
JMenuItem jMenuItem1;
// 子菜单组件
JMenuItem jMenuItem2;
// 子菜单组件
JFileChooser jFileChooser;
// 文件选择组件
JButton jButton1;
// 按钮组件
JButton jButton2;
// 按钮组件
JButton jButton3;
// 按钮组件
FileReader fileReader;
FileWriter fileWriter;
// 文件IO流
BufferedReader bufferedReader;
BufferedWriter bufferedWriter;
// 缓存IO流
public Notepad() {
jTextArea = new JTextArea();
// 创建一个文本区域
jScrollPane = new JScrollPane(jTextArea);
// 创建一个滚动条,并且加到文本居于上
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// 设置滚动条出现方式为“当需要时”
jToolBar = new JToolBar();
// 新建一个工具条
jButton1 = new JButton(new ImageIcon(Notepad.class.getResource("copy.png")));
// 新建一个按钮,图标为"copy.png"
jButton1.setActionCommand("复制");
// 设置按钮命令名称为“复制”
jButton1.addActionListener(this);
// 监听“复制”按钮的行为
jButton2 = new JButton(new ImageIcon(Notepad.class.getResource("paste.png")));
jButton2.setActionCommand("粘贴");
jButton2.addActionListener(this);
jButton3 = new JButton(new ImageIcon(Notepad.class.getResource("cut.png")));
jButton3.setActionCommand("剪切");
jButton3.addActionListener(this);
jToolBar.add(jButton1);
jToolBar.add(jButton2);
jToolBar.add(jButton3);
// 给工具条加上按钮
jMenuBar = new JMenuBar();
// 新建菜单栏
jMenu = new JMenu("文件");
// 新建菜单,名称为“文件”
jMenuItem1 = new JMenuItem("打开");
// 新建子菜单,名称为“打开”
jMenuItem1.setActionCommand("打开");
//设置子菜单命令名称为“打开”
jMenuItem1.addActionListener(this);
//监听“打开”子菜单的行为
jMenuItem2 = new JMenuItem("保存");
jMenuItem2.setActionCommand("保存");
jMenuItem2.addActionListener(this);
jMenuBar.add(jMenu);
jMenu.add(jMenuItem1);
jMenu.add(jMenuItem2);
this.setJMenuBar(jMenuBar);
//添加菜单栏
this.add(jToolBar, BorderLayout.NORTH);
//在窗口上方添加工具条
this.add(jScrollPane, BorderLayout.CENTER);
//添加窗口中间添加滚动条与文本框
this.setTitle("记事本");
//设置窗口名称为“记事本”
this.setSize(700, 800);
//设置大小
this.setLocationRelativeTo(null);
//设置窗口居中显示
this.setVisible(true);
//设置窗口可见
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//设置关闭窗口的方式为关闭进程
}
public void actionPerformed(ActionEvent e) {
//实现ActionListener接口
String str = e.getActionCommand();
//得到用户触发的命令
if (str.equals("打开")) {
jFileChooser = new JFileChooser();
//新建文件选择组件
jFileChooser.setDialogTitle("打开");
//设置标题为“打开”
jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//设置选择文件模式为“只可以选择文件”
jFileChooser.showOpenDialog(null);
//设置默认路径为null
jFileChooser.setVisible(true);
//设置窗口可见
String ads = jFileChooser.getSelectedFile().getAbsolutePath();
//得到选择文件的绝对地址
try {
fileReader = new FileReader(ads);
//打开文件
bufferedReader = new BufferedReader(fileReader);
String now, all = "";
while ((now = bufferedReader.readLine()) != null) {//读取文件
all += now + '\n';
}
jTextArea.setText(all);//将读取到的内容显示到文本框中
} catch (Exception e2) {
JOptionPane.showMessageDialog(this, e2.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
//抛出错误对话框,并显示错误信息
} finally {
try {
bufferedReader.close();//操作完要关闭文件,避免数据丢失
fileReader.close();
} catch (Exception e3) {
JOptionPane.showMessageDialog(this, e3.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
}
}
}
if (str.equals("保存")) {
jFileChooser = new JFileChooser();
jFileChooser.setDialogTitle("另存为");
jFileChooser.showOpenDialog(null);
jFileChooser.setVisible(true);
String ads = jFileChooser.getSelectedFile().getAbsolutePath();
try {
fileWriter = new FileWriter(ads);
bufferedWriter = new BufferedWriter(fileWriter);
String all = jTextArea.getText();
bufferedWriter.write(all);
} catch (Exception e2) {
JOptionPane.showMessageDialog(this, e2.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
} finally {
try {
bufferedWriter.close();
fileWriter.close();
} catch (Exception e3) {
JOptionPane.showMessageDialog(this, e3.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
}
}
}
if (str.equals("复制")) {
try {
jTextArea.copy();
} catch (Exception e2) {
JOptionPane.showMessageDialog(this, e2.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
}
}
if (str.equals("粘贴")) {
try {
jTextArea.paste();
} catch (Exception e2) {
JOptionPane.showMessageDialog(this, e2.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
}
}
if (str.equals("剪切")) {
try {
jTextArea.cut();
} catch (Exception e2) {
JOptionPane.showMessageDialog(this, e2.getMessage(), "警告", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
Notepad notepad = new Notepad();
}
}