用java 添加菜单和弹出式菜单制作一个简单文 本编辑器。 •菜单条中有“文件》和《编辑”两个菜单,每个菜 单中的内容如下面两张图所示。 利用FileDialog分别创建“打开文件对话框”和 “保存文件对话框” 实现文本文件的打开和保 存功能。 主窗口以JTextArea进行操作,单击右键打开 快捷菜单,实现文本的a考贝》和心粘贴》功能。
时间: 2024-03-14 12:46:14 浏览: 149
好的,我可以帮你解决这个问题。以下是Java代码实现一个简单的文本编辑器,其中包括菜单和弹出式菜单的添加以及打开、保存文件功能和文本的复制、粘贴功能。
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
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.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
public class SimpleTextEditor extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private JFileChooser fileChooser;
private JPopupMenu popupMenu;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SimpleTextEditor frame = new SimpleTextEditor();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SimpleTextEditor() {
setTitle("简单文本编辑器");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 菜单条
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
// 文件菜单
JMenu fileMenu = new JMenu("文件(F)");
fileMenu.setMnemonic('F');
menuBar.add(fileMenu);
// 打开文件菜单项
JMenuItem openMenuItem = new JMenuItem("打开(O)...");
openMenuItem.setMnemonic('O');
openMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
openMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
fileMenu.add(openMenuItem);
// 保存文件菜单项
JMenuItem saveMenuItem = new JMenuItem("保存(S)...");
saveMenuItem.setMnemonic('S');
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
fileMenu.add(saveMenuItem);
// 退出菜单项
JMenuItem exitMenuItem = new JMenuItem("退出(X)");
exitMenuItem.setMnemonic('X');
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl Q"));
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitMenuItem);
// 编辑菜单
JMenu editMenu = new JMenu("编辑(E)");
editMenu.setMnemonic('E');
menuBar.add(editMenu);
// 复制菜单项
JMenuItem copyMenuItem = new JMenuItem("复制(C)");
copyMenuItem.setMnemonic('C');
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl C"));
copyMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.copy();
}
});
editMenu.add(copyMenuItem);
// 粘贴菜单项
JMenuItem pasteMenuItem = new JMenuItem("粘贴(P)");
pasteMenuItem.setMnemonic('P');
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl V"));
pasteMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.paste();
}
});
editMenu.add(pasteMenuItem);
// 主面板
JPanel contentPane = new JPanel(new BorderLayout());
setContentPane(contentPane);
// 文本区域
textArea = new JTextArea();
textArea.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
popupMenu.show(textArea, e.getX(), e.getY());
}
}
});
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(scrollPane, BorderLayout.CENTER);
// 快捷菜单
popupMenu = new JPopupMenu();
JMenuItem copyMenuItem2 = new JMenuItem("复制(C)");
copyMenuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.copy();
}
});
popupMenu.add(copyMenuItem2);
JMenuItem pasteMenuItem2 = new JMenuItem("粘贴(P)");
pasteMenuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.paste();
}
});
popupMenu.add(pasteMenuItem2);
// 文件选择器
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
}
// 打开文件
private void openFile() {
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).append("\n");
}
reader.close();
textArea.setText(sb.toString());
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "打开文件失败:" + e.getMessage());
}
}
}
// 保存文件
private void saveFile() {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.close();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "保存文件失败:" + e.getMessage());
}
}
}
}
```
代码中使用了JMenuBar、JMenu、JMenuItem、JPopupMenu、JFileChooser、JTextArea、JScrollPane等Swing组件,通过addActionListener()方法为菜单项添加事件处理程序,实现打开、保存文件和文本的复制、粘贴功能。同时,添加了一个鼠标事件处理程序,实现了右键单击文本区域弹出快捷菜单的功能。
阅读全文