📅  最后修改于: 2023-12-03 15:31:34.414000             🧑  作者: Mango
Java 记事本是一个用 Java 语言编写的简单文本编辑器。它可以用来编辑纯文本文件,支持复制、粘贴、撤销、重做等基本操作,以及查找、替换、全选等高级操作。
以下是 Java 记事本的源代码片段:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class JavaNotepad {
private JFrame frame = new JFrame("Java 记事本");
private JTextArea textarea = new JTextArea();
private JMenuBar menubar = new JMenuBar();
private JMenu filemenu = new JMenu("文件");
private JMenuItem newfile = new JMenuItem("新建");
private JMenuItem openfile = new JMenuItem("打开");
private JMenuItem savefile = new JMenuItem("保存");
private JMenuItem saveasfile = new JMenuItem("另存为");
private JMenuItem exit = new JMenuItem("退出");
public JavaNotepad() {
// 设置菜单
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.add(savefile);
filemenu.add(saveasfile);
filemenu.addSeparator();
filemenu.add(exit);
menubar.add(filemenu);
frame.setJMenuBar(menubar);
// 设置文本区域
frame.add(textarea);
// 设置快捷键
newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
openfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
savefile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_DOWN_MASK));
// 设置事件处理
newfile.addActionListener(new NewFileActionListener());
openfile.addActionListener(new OpenFileActionListener());
savefile.addActionListener(new SaveFileActionListener());
saveasfile.addActionListener(new SaveAsFileActionListener());
exit.addActionListener(new ExitActionListener());
frame.addWindowListener(new FrameWindowListener());
// 设置窗口属性
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
}
// 新建文件的事件处理
class NewFileActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textarea.setText("");
}
}
// 打开文件的事件处理
class OpenFileActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
try {
BufferedReader reader = new BufferedReader(new FileReader(chooser.getSelectedFile()));
String line = "";
textarea.setText("");
while ((line = reader.readLine()) != null) {
textarea.append(line + "\n");
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
// 保存文件的事件处理
class SaveFileActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (filepath == null) {
saveAsFileAction();
} else {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filepath));
writer.write(textarea.getText());
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
// 另存为文件的事件处理
class SaveAsFileActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
saveAsFileAction();
}
}
// 退出程序的事件处理
class ExitActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int option = JOptionPane.showConfirmDialog(frame, "确定要退出吗?", "退出", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION) {
frame.dispose();
System.exit(0);
}
}
}
// 窗口关闭的事件处理
class FrameWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
exit.doClick();
}
}
// 另存为文件的具体实现
private void saveAsFileAction() {
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
try {
filepath = chooser.getSelectedFile().getAbsolutePath();
BufferedWriter writer = new BufferedWriter(new FileWriter(filepath));
writer.write(textarea.getText());
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new JavaNotepad();
}
}
Java 记事本的功能包括:
Java 记事本涉及到的技术点有: