java代码 用图形界面实现一个简单的文本记事本,能实现编辑、保存、另存为、查找、替换等功能
时间: 2023-07-10 17:20:09 浏览: 102
好的,以下是一个简单的Java代码,使用图形界面实现文本记事本的编辑、保存、另存为、查找和替换功能。其中,编辑功能包括新建、打开和保存文件,查找和替换功能使用Java的正则表达式。
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.filechooser.*;
public class TextEditor extends JFrame implements ActionListener {
private JTextArea area = new JTextArea(20, 120);
private JFileChooser dialog = new JFileChooser(System.getProperty("user.dir"));
private String currentFile = "Untitled";
private boolean changed = false;
public TextEditor() {
area.setFont(new Font("Monospaced", Font.PLAIN, 12));
JScrollPane scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(scroll, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem newFile = new JMenuItem("New");
newFile.addActionListener(this);
file.add(newFile);
JMenuItem openFile = new JMenuItem("Open");
openFile.addActionListener(this);
file.add(openFile);
JMenuItem saveFile = new JMenuItem("Save");
saveFile.addActionListener(this);
file.add(saveFile);
JMenuItem saveAsFile = new JMenuItem("Save As...");
saveAsFile.addActionListener(this);
file.add(saveAsFile);
file.addSeparator();
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
file.add(exit);
JMenu edit = new JMenu("Edit");
menuBar.add(edit);
JMenuItem find = new JMenuItem("Find");
find.addActionListener(this);
edit.add(find);
JMenuItem replace = new JMenuItem("Replace");
replace.addActionListener(this);
edit.add(replace);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
area.addKeyListener(k1);
setTitle(currentFile);
setVisible(true);
}
private KeyListener k1 = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
changed = true;
setTitle("*" + currentFile);
}
};
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("New")) {
new TextEditor();
} else if (cmd.equals("Open")) {
if (dialog.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
readInFile(dialog.getSelectedFile().getAbsolutePath());
}
} else if (cmd.equals("Save")) {
if (!currentFile.equals("Untitled")) {
saveToFile(currentFile);
} else {
saveAsFile();
}
} else if (cmd.equals("Save As...")) {
saveAsFile();
} else if (cmd.equals("Exit")) {
if (changed) {
int confirm = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit without saving?", "Confirm Exit", JOptionPane.YES_NO_CANCEL_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
dispose();
} else if (confirm == JOptionPane.NO_OPTION) {
saveAsFile();
dispose();
}
} else {
dispose();
}
} else if (cmd.equals("Find")) {
String find = JOptionPane.showInputDialog(null, "Find:");
if (find != null) {
Pattern pattern = Pattern.compile(find);
Matcher matcher = pattern.matcher(area.getText());
if (matcher.find()) {
area.setCaretPosition(matcher.start());
area.setSelectionStart(matcher.start());
area.setSelectionEnd(matcher.end());
}
}
} else if (cmd.equals("Replace")) {
String find = JOptionPane.showInputDialog(null, "Find:");
if (find != null) {
String replace = JOptionPane.showInputDialog(null, "Replace with:");
if (replace != null) {
Pattern pattern = Pattern.compile(find);
Matcher matcher = pattern.matcher(area.getText());
if (matcher.find()) {
area.replaceRange(replace, matcher.start(), matcher.end());
}
}
}
}
}
private void readInFile(String fileName) {
try {
FileReader r = new FileReader(fileName);
area.read(r, null);
r.close();
currentFile = fileName;
setTitle(currentFile);
changed = false;
} catch (IOException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "Could not open file: " + fileName, "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void saveToFile(String fileName) {
try {
FileWriter w = new FileWriter(fileName);
area.write(w);
w.close();
currentFile = fileName;
setTitle(currentFile);
changed = false;
} catch (IOException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "Could not save file: " + fileName, "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void saveAsFile() {
if (dialog.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
saveToFile(dialog.getSelectedFile().getAbsolutePath());
}
}
public static void main(String[] args) {
new TextEditor();
}
}
```
希望可以帮助到你!
阅读全文