使用Java语言设计一个java文本编辑器,能够实现基本的文本文件读取、保存、设置字体等功能。具有菜单、工具栏和状态栏。 实现查找、打印预览。为每个功能添加注释
时间: 2023-12-30 18:01:51 浏览: 79
Java做的文本编辑器
5星 · 资源好评率100%
好的,下面是使用Java语言设计的文本编辑器,可以实现基本的文本文件读取、保存、设置字体等功能,具有菜单、工具栏和状态栏,并实现了查找和打印预览功能。以下是代码及注释:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
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.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
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.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
public class TextEditor extends JFrame implements ActionListener {
private JTextArea textArea; //文本区域
private JTextField findField; //查找输入框
private JTextField replaceField; //替换输入框
private JLabel statusLabel; //状态栏标签
public TextEditor() {
initComponents();
}
//初始化组件
private void initComponents() {
//设置窗口标题
setTitle("文本编辑器");
//设置窗口图标
ImageIcon icon = new ImageIcon(getClass().getResource("icon.png"));
setIconImage(icon.getImage());
//设置窗口大小
setSize(600, 400);
//设置窗口居中显示
setLocationRelativeTo(null);
//设置窗口关闭按钮操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建菜单栏
JMenuBar menuBar = new JMenuBar();
//创建文件菜单
JMenu fileMenu = new JMenu("文件");
fileMenu.setMnemonic('F');
//创建新建菜单项
JMenuItem newMenuItem = new JMenuItem("新建", 'N');
newMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
newMenuItem.addActionListener(this);
fileMenu.add(newMenuItem);
//创建打开菜单项
JMenuItem openMenuItem = new JMenuItem("打开", 'O');
openMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
openMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
//创建保存菜单项
JMenuItem saveMenuItem = new JMenuItem("保存", 'S');
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
saveMenuItem.addActionListener(this);
fileMenu.add(saveMenuItem);
//创建另存为菜单项
JMenuItem saveAsMenuItem = new JMenuItem("另存为", 'A');
saveAsMenuItem.addActionListener(this);
fileMenu.add(saveAsMenuItem);
//添加分隔线
fileMenu.addSeparator();
//创建退出菜单项
JMenuItem exitMenuItem = new JMenuItem("退出", 'x');
exitMenuItem.addActionListener(this);
fileMenu.add(exitMenuItem);
//将文件菜单添加到菜单栏
menuBar.add(fileMenu);
//创建编辑菜单
JMenu editMenu = new JMenu("编辑");
editMenu.setMnemonic('E');
//创建撤销菜单项
JMenuItem undoMenuItem = new JMenuItem("撤销", 'U');
undoMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl Z"));
undoMenuItem.addActionListener(this);
editMenu.add(undoMenuItem);
//创建重做菜单项
JMenuItem redoMenuItem = new JMenuItem("重做", 'R');
redoMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl Y"));
redoMenuItem.addActionListener(this);
editMenu.add(redoMenuItem);
//添加分隔线
editMenu.addSeparator();
//创建剪切菜单项
JMenuItem cutMenuItem = new JMenuItem("剪切", 'T');
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl X"));
cutMenuItem.addActionListener(this);
editMenu.add(cutMenuItem);
//创建复制菜单项
JMenuItem copyMenuItem = new JMenuItem("复制", 'C');
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl C"));
copyMenuItem.addActionListener(this);
editMenu.add(copyMenuItem);
//创建粘贴菜单项
JMenuItem pasteMenuItem = new JMenuItem("粘贴", 'P');
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl V"));
pasteMenuItem.addActionListener(this);
editMenu.add(pasteMenuItem);
//添加分隔线
editMenu.addSeparator();
//创建查找菜单项
JMenuItem findMenuItem = new JMenuItem("查找", 'F');
findMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl F"));
findMenuItem.addActionListener(this);
editMenu.add(findMenuItem);
//创建替换菜单项
JMenuItem replaceMenuItem = new JMenuItem("替换", 'R');
replaceMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl H"));
replaceMenuItem.addActionListener(this);
editMenu.add(replaceMenuItem);
//将编辑菜单添加到菜单栏
menuBar.add(editMenu);
//创建格式菜单
JMenu formatMenu = new JMenu("格式");
formatMenu.setMnemonic('O');
//创建字体菜单项
JMenuItem fontMenuItem = new JMenuItem("字体", 'F');
fontMenuItem.addActionListener(this);
formatMenu.add(fontMenuItem);
//创建颜色菜单项
JMenuItem colorMenuItem = new JMenuItem("颜色", 'C');
colorMenuItem.addActionListener(this);
formatMenu.add(colorMenuItem);
//将格式菜单添加到菜单栏
menuBar.add(formatMenu);
//创建查看菜单
JMenu viewMenu = new JMenu("查看");
viewMenu.setMnemonic('V');
//创建状态栏菜单项
JMenuItem statusBarMenuItem = new JMenuItem("状态栏", 'S');
statusBarMenuItem.addActionListener(this);
viewMenu.add(statusBarMenuItem);
//将查看菜单添加到菜单栏
menuBar.add(viewMenu);
//创建帮助菜单
JMenu helpMenu = new JMenu("帮助");
helpMenu.setMnemonic('H');
//创建关于菜单项
JMenuItem aboutMenuItem = new JMenuItem("关于", 'A');
aboutMenuItem.addActionListener(this);
helpMenu.add(aboutMenuItem);
//将帮助菜单添加到菜单栏
menuBar.add(helpMenu);
//设置菜单栏
setJMenuBar(menuBar);
//创建工具栏
JPanel toolBar = new JPanel();
toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.X_AXIS));
//创建新建工具按钮
JButton newButton = new JButton(new ImageIcon(getClass().getResource("new.png")));
newButton.setToolTipText("新建");
newButton.addActionListener(this);
toolBar.add(newButton);
//创建打开工具按钮
JButton openButton = new JButton(new ImageIcon(getClass().getResource("open.png")));
openButton.setToolTipText("打开");
openButton.addActionListener(this);
toolBar.add(openButton);
//创建保存工具按钮
JButton saveButton = new JButton(new ImageIcon(getClass().getResource("save.png")));
saveButton.setToolTipText("保存");
saveButton.addActionListener(this);
toolBar.add(saveButton);
//添加分隔符
toolBar.add(Box.createRigidArea(new Dimension(10, 0)));
//创建剪切工具按钮
JButton cutButton = new JButton(new ImageIcon(getClass().getResource("cut.png")));
cutButton.setToolTipText("剪切");
cutButton.addActionListener(this);
toolBar.add(cutButton);
//创建复制工具按钮
JButton copyButton = new JButton(new ImageIcon(getClass().getResource("copy.png")));
copyButton.setToolTipText("复制");
copyButton.addActionListener(this);
toolBar.add(copyButton);
//创建粘贴工具按钮
JButton pasteButton = new JButton(new ImageIcon(getClass().getResource("paste.png")));
pasteButton.setToolTipText("粘贴");
pasteButton.addActionListener(this);
toolBar.add(pasteButton);
//添加分隔符
toolBar.add(Box.createRigidArea(new Dimension(10, 0)));
//创建查找工具按钮
JButton findButton = new JButton(new ImageIcon(getClass().getResource("find.png")));
findButton.setToolTipText("查找");
findButton.addActionListener(this);
toolBar.add(findButton);
//创建替换工具按钮
JButton replaceButton = new JButton(new ImageIcon(getClass().getResource("replace.png")));
replaceButton.setToolTipText("替换");
replaceButton.addActionListener(this);
toolBar.add(replaceButton);
//设置工具栏
add(toolBar, BorderLayout.NORTH);
//创建文本区域
textArea = new JTextArea();
textArea.setFont(new Font("宋体", Font.PLAIN, 14));
//创建滚动面板
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
//创建状态栏
JPanel statusBar = new JPanel();
statusBar.setLayout(new BoxLayout(statusBar, BoxLayout.X_AXIS));
statusLabel = new JLabel("字符数: 0 行数: 0");
statusBar.add(statusLabel);
add(statusBar, BorderLayout.SOUTH);
}
//实现菜单、按钮等组件的动作监听
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
//新建
case "新建":
newFile();
break;
//打开
case "打开":
openFile();
break;
//保存
case "保存":
saveFile();
break;
//另存为
case "另存为":
saveAsFile();
break;
//退出
case "退出":
System.exit(0);
break;
//撤销
case "撤销":
if (textArea.canUndo()) {
textArea.undo();
}
break;
//重做
case "重做":
if (textArea.can
阅读全文