仿照windows记事本,制作一个记事本软件,实现纯文本文件的新建、打开、保存、另存、复制、粘贴、查找、替换、关于等等功能;关闭软件是弹出对话框提示是否把修改保存,根据用户选择更新磁盘文件内容。用java实现
时间: 2023-07-10 13:04:59 浏览: 130
java记事本(与windows的记事本一样的功能)
3星 · 编辑精心推荐
好的,我可以为您提供一些思路和代码示例。
首先,我们需要创建一个主界面窗口。以下是一个简单的示例代码:
```java
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyNotepad extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private JLabel statusLabel;
private JFileChooser fileChooser;
private File currentFile;
private boolean isModified;
private JTextField searchField, replaceField;
private JButton searchButton, replaceButton, replaceAllButton;
public MyNotepad() {
super("Java记事本");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 500);
setLocationRelativeTo(null);
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
// 创建文件菜单
JMenu fileMenu = new JMenu("文件");
fileMenu.setMnemonic('F');
JMenuItem newItem = new JMenuItem("新建", 'N');
newItem.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
newItem.addActionListener(this);
fileMenu.add(newItem);
JMenuItem openItem = new JMenuItem("打开", 'O');
openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
openItem.addActionListener(this);
fileMenu.add(openItem);
JMenuItem saveItem = new JMenuItem("保存", 'S');
saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
saveItem.addActionListener(this);
fileMenu.add(saveItem);
JMenuItem saveAsItem = new JMenuItem("另存为", 'A');
saveAsItem.addActionListener(this);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("退出", 'X');
exitItem.addActionListener(this);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
// 创建编辑菜单
JMenu editMenu = new JMenu("编辑");
editMenu.setMnemonic('E');
JMenuItem copyItem = new JMenuItem("复制", 'C');
copyItem.setAccelerator(KeyStroke.getKeyStroke("ctrl C"));
copyItem.addActionListener(this);
editMenu.add(copyItem);
JMenuItem pasteItem = new JMenuItem("粘贴", 'P');
pasteItem.setAccelerator(KeyStroke.getKeyStroke("ctrl V"));
pasteItem.addActionListener(this);
editMenu.add(pasteItem);
JMenuItem findItem = new JMenuItem("查找", 'F');
findItem.setAccelerator(KeyStroke.getKeyStroke("ctrl F"));
findItem.addActionListener(this);
editMenu.add(findItem);
JMenuItem replaceItem = new JMenuItem("替换", 'R');
replaceItem.setAccelerator(KeyStroke.getKeyStroke("ctrl H"));
replaceItem.addActionListener(this);
editMenu.add(replaceItem);
menuBar.add(editMenu);
// 创建帮助菜单
JMenu helpMenu = new JMenu("帮助");
JMenuItem aboutItem = new JMenuItem("关于", 'A');
aboutItem.addActionListener(this);
helpMenu.add(aboutItem);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
// 创建工具栏
JPanel toolBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton newButton = new JButton("新建");
newButton.addActionListener(this);
toolBar.add(newButton);
JButton openButton = new JButton("打开");
openButton.addActionListener(this);
toolBar.add(openButton);
JButton saveButton = new JButton("保存");
saveButton.addActionListener(this);
toolBar.add(saveButton);
JButton copyButton = new JButton("复制");
copyButton.addActionListener(this);
toolBar.add(copyButton);
JButton pasteButton = new JButton("粘贴");
pasteButton.addActionListener(this);
toolBar.add(pasteButton);
JButton findButton = new JButton("查找");
findButton.addActionListener(this);
toolBar.add(findButton);
JButton replaceButton = new JButton("替换");
replaceButton.addActionListener(this);
toolBar.add(replaceButton);
// 创建状态栏
statusLabel = new JLabel("就绪");
statusLabel.setPreferredSize(new Dimension(getWidth(), 20));
statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
statusLabel.setBorder(BorderFactory.createLoweredBevelBorder());
// 创建文本域
textArea = new JTextArea();
textArea.setFont(new Font("宋体", Font.PLAIN, 16));
JScrollPane scrollPane = new JScrollPane(textArea);
// 创建查找替换面板
JPanel searchPanel = new JPanel(new GridLayout(1, 5));
Border border = BorderFactory.createEtchedBorder();
searchPanel.setBorder(BorderFactory.createTitledBorder(border, "查找/替换"));
JLabel searchLabel = new JLabel("查找:");
searchField = new JTextField();
searchPanel.add(searchLabel);
searchPanel.add(searchField);
searchButton = new JButton("查找下一个");
searchButton.addActionListener(this);
searchPanel.add(searchButton);
JLabel replaceLabel = new JLabel("替换为:");
replaceField = new JTextField();
searchPanel.add(replaceLabel);
replaceButton = new JButton("替换");
replaceButton.addActionListener(this);
searchPanel.add(replaceButton);
replaceAllButton = new JButton("全部替换");
replaceAllButton.addActionListener(this);
searchPanel.add(replaceAllButton);
// 添加组件到容器中
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
contentPane.add(statusLabel, BorderLayout.SOUTH);
contentPane.add(searchPanel, BorderLayout.EAST);
// 初始化状态
isModified = false;
setTitle("未命名" + " - Java记事本");
// 创建文件选择器
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("文本文件 (*.txt)", "txt"));
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "新建":
doNew();
break;
case "打开":
doOpen();
break;
case "保存":
doSave();
break;
case "另存为":
doSaveAs();
break;
case "退出":
doExit();
break;
case "复制":
doCopy();
break;
case "粘贴":
doPaste();
break;
case "查找":
doFind();
break;
case "替换":
doReplace();
break;
case "查找下一个":
doFindNext();
break;
case "替换":
doReplace();
break;
case "全部替换":
doReplaceAll();
break;
case "关于":
doAbout();
break;
}
}
// 新建文件
private void doNew() {
if (isModified) {
int result = JOptionPane.showConfirmDialog(this, "是否保存更改?", "保存", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
doSave();
} else if (result == JOptionPane.CANCEL_OPTION) {
return;
}
}
currentFile = null;
textArea.setText("");
setTitle("未命名" + " - Java记事本");
isModified = false;
}
// 打开文件
private void doOpen() {
if (isModified) {
int result = JOptionPane.showConfirmDialog(this, "是否保存更改?", "保存", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
doSave();
} else if (result == JOptionPane.CANCEL_OPTION) {
return;
}
}
int result = fileChooser.showOpenDialog(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);
sb.append(System.lineSeparator());
}
textArea.setText(sb.toString());
currentFile = file;
setTitle(currentFile.getName() + " - Java记事本");
isModified = false;
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
// 保存文件
private void doSave() {
if (currentFile == null) {
doSaveAs();
} else {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile))) {
writer.write(textArea.getText());
setTitle(currentFile.getName() + " - Java记事本");
isModified = false;
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
// 另存为文件
private void doSaveAs() {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (!file.getName().endsWith(".txt")) {
file = new File(file.getAbsolutePath() + ".txt");
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(textArea.getText());
currentFile = file;
setTitle(currentFile.getName() + " - Java记事本");
isModified = false;
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
// 退出程序
private void doExit() {
if (isModified) {
int result = JOptionPane.showConfirmDialog(this, "是否保存更改?", "保存", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
doSave();
} else if (result == JOptionPane.CANCEL_OPTION) {
return;
}
}
System.exit(0);
}
// 复制
private void doCopy() {
textArea.copy();
}
// 粘贴
private void doPaste() {
textArea.paste();
}
// 查找
private void doFind() {
searchField.requestFocus();
}
// 替换
private void doReplace() {
replaceField.requestFocus();
}
// 查找下一个
private void doFindNext() {
String searchText = searchField.getText();
String text = textArea.getText();
int index = text.indexOf(searchText, textArea.getCaretPosition());
if (index >= 0) {
textArea.select(index, index + searchText.length());
} else {
JOptionPane.showMessageDialog(this, "找不到\"" + searchText + "\"", "查找", JOptionPane.INFORMATION_MESSAGE);
}
}
// 替换
private void doReplaceAll() {
String searchText = searchField.getText();
String replaceText = replaceField.getText();
String text = textArea.getText();
text = text.replaceAll(searchText, replaceText);
textArea.setText(text);
}
// 关于
private void doAbout() {
JOptionPane.showMessageDialog(this, "Java记事本 v1.0\n作者:xxx", "关于", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
new MyNotepad();
}
}
```
在这个示例中,我们实现了新建、打开、保存、另存、复制、粘贴、查找、替换、关于等功能,并且在关闭软件时弹出对话框提示是否保存修改。同时,我们还添加了工具栏和状态栏,方便用户操作和查看文件状态。
需要注意的是,我们使用了文件选择器来让用户选择文件,同时限定了文件类型为纯文本文件。在保存文件时,如果用户没有指定文件名或者选择的文件名不符合要求,我们会自动添加".txt"后缀以保证文件类型正确。
在查找和替换功能中,我们使用了Java的字符串查找和替换方法,这些方法都非常方便实用。同时,我们也可以使用正则表达式来进行更加灵活的匹配和替换。
希望这个示例能够帮助您完成记事本软件的制作。
阅读全文