帮我用java写下面这道题:生成一个简单文本编辑器,将生成的文本存入文件,如果有打开、存盘、关闭等菜单更佳。GUI界面可以自己灵活设计,同时实现窗体关闭退出功能。
时间: 2024-02-21 10:02:12 浏览: 71
下面是一个简单的文本编辑器的 Java 代码:
```java
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
public class TextEditor extends JFrame {
private JTextArea textArea;
private JFileChooser fileChooser;
private File currentFile;
public TextEditor() {
setTitle("Text Editor");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
// 创建菜单项
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem saveAsItem = new JMenuItem("Save As");
JMenuItem exitItem = new JMenuItem("Exit");
// 添加菜单项到菜单栏
menuBar.add(openItem);
menuBar.add(saveItem);
menuBar.add(saveAsItem);
menuBar.add(exitItem);
// 注册菜单项事件监听器
openItem.addActionListener(new OpenActionListener());
saveItem.addActionListener(new SaveActionListener());
saveAsItem.addActionListener(new SaveAsActionListener());
exitItem.addActionListener(new ExitActionListener());
// 创建文本编辑区域
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
// 添加文本编辑区域到窗口
Container contentPane = getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
// 创建文件选择器,用于打开和保存文件
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
}
// 打开文件事件监听器
private class OpenActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int result = fileChooser.showOpenDialog(TextEditor.this);
if (result == JFileChooser.APPROVE_OPTION) {
try {
currentFile = fileChooser.getSelectedFile();
byte[] bytes = Files.readAllBytes(Paths.get(currentFile.getPath()));
String content = new String(bytes);
textArea.setText(content);
} catch (IOException ex) {
JOptionPane.showMessageDialog(TextEditor.this, "Failed to open file: " + ex.getMessage());
}
}
}
}
// 保存文件事件监听器
private class SaveActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (currentFile == null) {
saveAs();
} else {
try {
String content = textArea.getText();
BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile));
writer.write(content);
writer.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(TextEditor.this, "Failed to save file: " + ex.getMessage());
}
}
}
}
// 另存为事件监听器
private class SaveAsActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
saveAs();
}
}
// 退出事件监听器
private class ExitActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
// 另存为方法
private void saveAs() {
int result = fileChooser.showSaveDialog(TextEditor.this);
if (result == JFileChooser.APPROVE_OPTION) {
try {
currentFile = fileChooser.getSelectedFile();
String content = textArea.getText();
BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile));
writer.write(content);
writer.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(TextEditor.this, "Failed to save file: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
TextEditor editor = new TextEditor();
editor.setVisible(true);
}
}
```
这个文本编辑器具有打开、存盘、另存为、关闭等菜单,可以将文本存储到文件中。用户可以使用菜单或快捷键来执行这些操作。界面比较简单,只有一个文本编辑区域和一个菜单栏。用户可以在编辑区域输入文本,然后使用菜单或快捷键将文本存储到文件中。如果用户关闭窗口,程序将退出。
阅读全文