制作一个记事本软件,实现纯文本文件的新建、打开、保存、另存、复制、粘贴、查找、替换、关于等等功能;关闭软件是弹出对话框提示是否把修改保存,根据用户选择更新磁盘文件内容。java直接给出代码
时间: 2023-06-24 16:04:47 浏览: 85
c#记事本源码(新建、保存、另存为、打开、页面设置、打印、退出、剪贴、复制、黏贴、撤销、查找、替换、全选、时间、自动换行、字体等功能)
以下是一个简单的记事本软件的Java代码,实现了基本的功能:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Notepad implements ActionListener, KeyListener {
private JFrame frame;
private JTextArea textArea;
private JFileChooser fileChooser;
private File currentFile;
private boolean isModified;
public Notepad() {
frame = new JFrame("Notepad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea();
textArea.addKeyListener(this);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.getContentPane().add(scrollPane);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
newMenuItem.addActionListener(this);
fileMenu.add(newMenuItem);
JMenuItem openMenuItem = new JMenuItem("Open...");
openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
openMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
saveMenuItem.addActionListener(this);
fileMenu.add(saveMenuItem);
JMenuItem saveAsMenuItem = new JMenuItem("Save As...");
saveAsMenuItem.addActionListener(this);
fileMenu.add(saveAsMenuItem);
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(this);
fileMenu.add(exitMenuItem);
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
menuBar.add(editMenu);
JMenuItem copyMenuItem = new JMenuItem("Copy");
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
copyMenuItem.addActionListener(this);
editMenu.add(copyMenuItem);
JMenuItem pasteMenuItem = new JMenuItem("Paste");
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
pasteMenuItem.addActionListener(this);
editMenu.add(pasteMenuItem);
JMenuItem findMenuItem = new JMenuItem("Find");
findMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
findMenuItem.addActionListener(this);
editMenu.add(findMenuItem);
JMenuItem replaceMenuItem = new JMenuItem("Replace");
replaceMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK));
replaceMenuItem.addActionListener(this);
editMenu.add(replaceMenuItem);
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic(KeyEvent.VK_H);
menuBar.add(helpMenu);
JMenuItem aboutMenuItem = new JMenuItem("About");
aboutMenuItem.addActionListener(this);
helpMenu.add(aboutMenuItem);
fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
fileChooser.setFileFilter(filter);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
isModified = false;
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
new Notepad();
}
});
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("New")) {
newFile();
} else if (command.equals("Open...")) {
openFile();
} else if (command.equals("Save")) {
saveFile();
} else if (command.equals("Save As...")) {
saveFileAs();
} else if (command.equals("Exit")) {
exit();
} else if (command.equals("Copy")) {
textArea.copy();
} else if (command.equals("Paste")) {
textArea.paste();
} else if (command.equals("Find")) {
find();
} else if (command.equals("Replace")) {
replace();
} else if (command.equals("About")) {
JOptionPane.showMessageDialog(frame, "Notepad v1.0", "About Notepad", JOptionPane.INFORMATION_MESSAGE);
}
}
public void keyPressed(KeyEvent e) {
isModified = true;
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
private void newFile() {
if (isModified) {
int result = JOptionPane.showConfirmDialog(frame, "Do you want to save changes?", "Save Changes",
JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
saveFile();
} else if (result == JOptionPane.CANCEL_OPTION) {
return;
}
}
textArea.setText("");
currentFile = null;
isModified = false;
}
private void openFile() {
if (isModified) {
int result = JOptionPane.showConfirmDialog(frame, "Do you want to save changes?", "Save Changes",
JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
saveFile();
} else if (result == JOptionPane.CANCEL_OPTION) {
return;
}
}
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
textArea.setText(sb.toString());
currentFile = file;
isModified = false;
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "Error opening file: " + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile() {
if (currentFile == null) {
saveFileAs();
} else {
try {
FileWriter writer = new FileWriter(currentFile);
writer.write(textArea.getText());
writer.close();
isModified = false;
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "Error saving file: " + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFileAs() {
int result = fileChooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (file.exists()) {
int confirmResult = JOptionPane.showConfirmDialog(frame, "File already exists. Overwrite?",
"Confirm Overwrite", JOptionPane.YES_NO_OPTION);
if (confirmResult != JOptionPane.YES_OPTION) {
return;
}
}
try {
FileWriter writer = new FileWriter(file);
writer.write(textArea.getText());
writer.close();
currentFile = file;
isModified = false;
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "Error saving file: " + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
private void exit() {
if (isModified) {
int result = JOptionPane.showConfirmDialog(frame, "Do you want to save changes?", "Save Changes",
JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
saveFile();
} else if (result == JOptionPane.CANCEL_OPTION) {
return;
}
}
System.exit(0);
}
private void find() {
String text = JOptionPane.showInputDialog(frame, "Find:");
if (text != null) {
String allText = textArea.getText();
int index = allText.indexOf(text);
if (index == -1) {
JOptionPane.showMessageDialog(frame, "Not found.", "Find", JOptionPane.INFORMATION_MESSAGE);
} else {
textArea.setCaretPosition(index);
textArea.setSelectionStart(index);
textArea.setSelectionEnd(index + text.length());
}
}
}
private void replace() {
String findText = JOptionPane.showInputDialog(frame, "Find:");
if (findText == null) {
return;
}
String replaceText = JOptionPane.showInputDialog(frame, "Replace with:");
if (replaceText == null) {
return;
}
String allText = textArea.getText();
int index = allText.indexOf(findText);
if (index == -1) {
JOptionPane.showMessageDialog(frame, "Not found.", "Replace", JOptionPane.INFORMATION_MESSAGE);
} else {
textArea.setCaretPosition(index);
textArea.setSelectionStart(index);
textArea.setSelectionEnd(index + findText.length());
textArea.replaceSelection(replaceText);
}
}
}
```
该记事本软件使用了Java Swing界面库来创建GUI,并使用了Java IO库来进行文件操作。
阅读全文