Java 编写一个记事本软件,实现纯文本文件的新建、打开、保存、另存、复制、粘贴、查找、替换、关于等等功能;关闭软件是弹出对话框提示是否把修改保存的代码
时间: 2023-10-01 17:07:56 浏览: 94
以下是一个简单的记事本软件实现的示例代码,其中包括了新建、打开、保存、另存、复制、粘贴、查找、替换等功能,以及关闭软件时弹出对话框提示是否保存修改。
```java
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Notepad extends JFrame {
private JTextArea textArea;
private JFileChooser fileChooser;
private boolean isModified = false;
private String filePath = "";
public Notepad() {
setTitle("记事本");
setSize(800, 600);
setLocationRelativeTo(null);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("文件(F)");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("新建(N)");
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
newMenuItem.addActionListener(new NewActionListener());
fileMenu.add(newMenuItem);
JMenuItem openMenuItem = new JMenuItem("打开(O)");
openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
openMenuItem.addActionListener(new OpenActionListener());
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem = new JMenuItem("保存(S)");
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
saveMenuItem.addActionListener(new SaveActionListener());
fileMenu.add(saveMenuItem);
JMenuItem saveAsMenuItem = new JMenuItem("另存为(A)");
saveAsMenuItem.addActionListener(new SaveAsActionListener());
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
JMenuItem exitMenuItem = new JMenuItem("退出(X)");
exitMenuItem.addActionListener(new ExitActionListener());
fileMenu.add(exitMenuItem);
JMenu editMenu = new JMenu("编辑(E)");
editMenu.setMnemonic(KeyEvent.VK_E);
menuBar.add(editMenu);
JMenuItem cutMenuItem = new JMenuItem(new DefaultEditorKit.CutAction());
cutMenuItem.setText("剪切(T)");
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));
editMenu.add(cutMenuItem);
JMenuItem copyMenuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
copyMenuItem.setText("复制(C)");
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
editMenu.add(copyMenuItem);
JMenuItem pasteMenuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
pasteMenuItem.setText("粘贴(P)");
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK));
editMenu.add(pasteMenuItem);
editMenu.addSeparator();
JMenuItem findMenuItem = new JMenuItem("查找(F)");
findMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_DOWN_MASK));
findMenuItem.addActionListener(new FindActionListener());
editMenu.add(findMenuItem);
JMenuItem replaceMenuItem = new JMenuItem("替换(R)");
replaceMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_DOWN_MASK));
replaceMenuItem.addActionListener(new ReplaceActionListener());
editMenu.add(replaceMenuItem);
JMenu helpMenu = new JMenu("帮助(H)");
helpMenu.setMnemonic(KeyEvent.VK_H);
menuBar.add(helpMenu);
JMenuItem aboutMenuItem = new JMenuItem("关于记事本(A)");
aboutMenuItem.addActionListener(new AboutActionListener());
helpMenu.add(aboutMenuItem);
fileChooser = new JFileChooser();
FileNameExtensionFilter txtFilter = new FileNameExtensionFilter("文本文件(*.txt)", "txt");
fileChooser.setFileFilter(txtFilter);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (isModified) {
int choice = JOptionPane.showConfirmDialog(Notepad.this, "是否保存修改?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION) {
saveFile();
} else if (choice == JOptionPane.CANCEL_OPTION) {
return;
}
}
dispose();
}
});
}
private class NewActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (isModified) {
int choice = JOptionPane.showConfirmDialog(Notepad.this, "是否保存修改?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION) {
saveFile();
} else if (choice == JOptionPane.CANCEL_OPTION) {
return;
}
}
textArea.setText("");
isModified = false;
filePath = "";
}
}
private class OpenActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (isModified) {
int choice = JOptionPane.showConfirmDialog(Notepad.this, "是否保存修改?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION) {
saveFile();
} else if (choice == JOptionPane.CANCEL_OPTION) {
return;
}
}
int result = fileChooser.showOpenDialog(Notepad.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
textArea.setText(sb.toString());
isModified = false;
filePath = file.getAbsolutePath();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
private class SaveActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
saveFile();
}
}
private class SaveAsActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int result = fileChooser.showSaveDialog(Notepad.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.flush();
writer.close();
isModified = false;
filePath = file.getAbsolutePath();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
private class ExitActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (isModified) {
int choice = JOptionPane.showConfirmDialog(Notepad.this, "是否保存修改?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION) {
saveFile();
} else if (choice == JOptionPane.CANCEL_OPTION) {
return;
}
}
dispose();
}
}
private class FindActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = JOptionPane.showInputDialog(Notepad.this, "查找内容:", "查找", JOptionPane.PLAIN_MESSAGE);
if (str != null) {
String text = textArea.getText();
int index = text.indexOf(str);
if (index != -1) {
textArea.setCaretPosition(index);
textArea.select(index, index + str.length());
} else {
JOptionPane.showMessageDialog(Notepad.this, "没有找到 \"" + str + "\"", "查找", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
private class ReplaceActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String findStr = JOptionPane.showInputDialog(Notepad.this, "查找内容:", "替换", JOptionPane.PLAIN_MESSAGE);
if (findStr != null) {
String replaceStr = JOptionPane.showInputDialog(Notepad.this, "替换为:", "替换", JOptionPane.PLAIN_MESSAGE);
if (replaceStr != null) {
String text = textArea.getText();
int index = text.indexOf(findStr);
if (index != -1) {
textArea.replaceRange(replaceStr, index, index + findStr.length());
isModified = true;
} else {
JOptionPane.showMessageDialog(Notepad.this, "没有找到 \"" + findStr + "\"", "替换", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
private class AboutActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Notepad.this, "Java记事本\n\n作者:xxx\n版本:1.0", "关于记事本", JOptionPane.INFORMATION_MESSAGE);
}
}
private void saveFile() {
if (filePath.isEmpty()) {
int result = fileChooser.showSaveDialog(Notepad.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.flush();
writer.close();
isModified = false;
filePath = file.getAbsolutePath();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else {
File file = new File(filePath);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.flush();
writer.close();
isModified = false;
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
Notepad notepad = new Notepad();
notepad.setVisible(true);
}
}
```
在上述代码中,我们使用了 Swing 中的 JTextArea、JScrollPane、JMenuBar、JMenu、JMenuItem、JFileChooser 等组件实现了一个简单的记事本软件。在关闭软件时,我们使用了 JOptionPane.showConfirmDialog() 方法弹出一个对话框提示用户是否保存修改。在保存文件时,我们先判断文件路径是否为空,如果为空则弹出保存文件对话框,如果不为空则直接保存文件。在查找和替换功能中,我们使用了 JTextArea 的 indexOf() 和 replaceRange() 方法实现了查找和替换文本的功能。
阅读全文