使用Java设计一个简单的文本编辑器,实现类似Windows的记事本打的简单功能, 具有菜单、工具栏和状态栏。要求对每段代码注释

时间: 2024-03-24 19:41:33 浏览: 17
好的,以下是代码实现: ```java import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; 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.ImageIcon; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuBar; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JToolBar; import javax.swing.filechooser.FileNameExtensionFilter; /** * A simple text editor implemented in Java using Swing * * @author [Your Name] * */ public class TextEditor extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JTextArea textArea; private JLabel statusBar; private String fileName; private boolean saved; /** * Constructor for TextEditor class */ public TextEditor() { super("Text Editor"); // Setting up menu bar JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); // Setting up file menu JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); // Adding menu items to file menu JMenuItem newMenuItem = new JMenuItem("New"); newMenuItem.addActionListener(this); fileMenu.add(newMenuItem); JMenuItem openMenuItem = new JMenuItem("Open"); openMenuItem.addActionListener(this); fileMenu.add(openMenuItem); JMenuItem saveMenuItem = new JMenuItem("Save"); saveMenuItem.addActionListener(this); fileMenu.add(saveMenuItem); JMenuItem saveAsMenuItem = new JMenuItem("Save As"); saveAsMenuItem.addActionListener(this); fileMenu.add(saveAsMenuItem); JMenuItem exitMenuItem = new JMenuItem("Exit"); exitMenuItem.addActionListener(this); fileMenu.add(exitMenuItem); // Setting up edit menu JMenu editMenu = new JMenu("Edit"); menuBar.add(editMenu); // Adding menu items to edit menu JMenuItem cutMenuItem = new JMenuItem("Cut"); cutMenuItem.addActionListener(this); editMenu.add(cutMenuItem); JMenuItem copyMenuItem = new JMenuItem("Copy"); copyMenuItem.addActionListener(this); editMenu.add(copyMenuItem); JMenuItem pasteMenuItem = new JMenuItem("Paste"); pasteMenuItem.addActionListener(this); editMenu.add(pasteMenuItem); // Setting up toolbar JToolBar toolBar = new JToolBar(); add(toolBar, BorderLayout.PAGE_START); // Adding buttons to toolbar ImageIcon newIcon = new ImageIcon("icons/new.png"); JButton newButton = new JButton(newIcon); newButton.addActionListener(this); toolBar.add(newButton); ImageIcon openIcon = new ImageIcon("icons/open.png"); JButton openButton = new JButton(openIcon); openButton.addActionListener(this); toolBar.add(openButton); ImageIcon saveIcon = new ImageIcon("icons/save.png"); JButton saveButton = new JButton(saveIcon); saveButton.addActionListener(this); toolBar.add(saveButton); ImageIcon cutIcon = new ImageIcon("icons/cut.png"); JButton cutButton = new JButton(cutIcon); cutButton.addActionListener(this); toolBar.add(cutButton); ImageIcon copyIcon = new ImageIcon("icons/copy.png"); JButton copyButton = new JButton(copyIcon); copyButton.addActionListener(this); toolBar.add(copyButton); ImageIcon pasteIcon = new ImageIcon("icons/paste.png"); JButton pasteButton = new JButton(pasteIcon); pasteButton.addActionListener(this); toolBar.add(pasteButton); // Setting up text area textArea = new JTextArea(); textArea.setFont(new Font("Arial", Font.PLAIN, 20)); textArea.setBorder(BorderFactory.createLineBorder(Color.BLACK)); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); // Setting up status bar statusBar = new JLabel("Ready"); add(statusBar, BorderLayout.SOUTH); // Setting up frame setSize(800, 600); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); // Initializing fields fileName = "Untitled"; saved = true; } /** * Main method for TextEditor class * * @param args */ public static void main(String[] args) { new TextEditor(); } /** * Method to handle actionPerformed events */ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); switch (command) { case "New": newFile(); break; case "Open": openFile(); break; case "Save": saveFile(); break; case "Save As": saveFileAs(); break; case "Exit": exit(); break; case "Cut": textArea.cut(); break; case "Copy": textArea.copy(); break; case "Paste": textArea.paste(); break; } } /** * Method to create a new file */ private void newFile() { if (!saved) { int option = JOptionPane.showConfirmDialog(this, "Do you want to save changes to " + fileName + "?", "Save Changes", JOptionPane.YES_NO_CANCEL_OPTION); if (option == JOptionPane.YES_OPTION) { saveFile(); } else if (option == JOptionPane.CANCEL_OPTION) { return; } } textArea.setText(""); fileName = "Untitled"; saved = true; statusBar.setText("New file created"); } /** * Method to open a file */ private void openFile() { if (!saved) { int option = JOptionPane.showConfirmDialog(this, "Do you want to save changes to " + fileName + "?", "Save Changes", JOptionPane.YES_NO_CANCEL_OPTION); if (option == JOptionPane.YES_OPTION) { saveFile(); } else if (option == JOptionPane.CANCEL_OPTION) { return; } } JFileChooser fileChooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt"); fileChooser.setFileFilter(filter); int option = fileChooser.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); fileName = file.getName(); try { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } reader.close(); textArea.setText(sb.toString()); saved = true; statusBar.setText("File opened: " + fileName); } catch (IOException e) { JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } } /** * Method to save a file */ private void saveFile() { if (saved) { return; } if (fileName.equals("Untitled")) { saveFileAs(); } else { try { BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); writer.write(textArea.getText()); writer.close(); saved = true; statusBar.setText("File saved: " + fileName); } catch (IOException e) { JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } } /** * Method to save a file with a new name */ private void saveFileAs() { JFileChooser fileChooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt"); fileChooser.setFileFilter(filter); int option = fileChooser.showSaveDialog(this); if (option == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); fileName = file.getName(); if (!fileName.endsWith(".txt")) { fileName += ".txt"; } try { BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(textArea.getText()); writer.close(); saved = true; statusBar.setText("File saved: " + fileName); } catch (IOException e) { JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } } /** * Method to exit the program */ private void exit() { if (!saved) { int option = JOptionPane.showConfirmDialog(this, "Do you want to save changes to " + fileName + "?", "Save Changes", JOptionPane.YES_NO_CANCEL_OPTION); if (option == JOptionPane.YES_OPTION) { saveFile(); } else if (option == JOptionPane.CANCEL_OPTION) { return; } } System.exit(0); } } ``` 此代码实现了一个简单的文本编辑器,具有菜单、工具栏和状态栏。可以打开、编辑、保存和另存为文本文件。还可以剪切、复制和粘贴文本。状态栏显示文件的状态,以及操作的结果。

相关推荐

最新推荐

recommend-type

JAVA简单记事本程序设计实验报告(带源码)

JAVA简单记事本程序设计实验报告(带源码) ...4.功能要求: 编写一个简单记事本程序,要求包含以下菜单项 文件 新建、打开、保存、退出 编辑 剪切、拷贝、粘贴、查找 帮助 关于 5.实现文件和编辑菜单里的功能
recommend-type

文本编辑器/记事本java源码

功能完备的文本编辑器,具有打开、保存、新建,粘贴、复制等功能;具有一个工具条。编写一个类似 Windows 记事本的简单程序,该程序能够创建新文件、读取已经存在的文件、查看和修改文本以及保存为文本文件。
recommend-type

记事本的设计与功能实现 Java课程设计报告 源代码

主要用到的是图形界面,可以在文本编辑框里进行编辑,在菜单选项里进行打开、保存、另存为、查找、替换、等功能。界面上的按钮有文件、编辑、工具和帮助。还添加有工具条,工具条中有按钮复制、粘贴和全选。
recommend-type

用 Windows “记事本”创建一个文本文件,其中每行包含一段英文,试读出文件的全部内容,并判断:(1)该文本文件共有多少行?(2)文件中以大写字母P开头的有多少行?(3)一行包含字

题目:用 Windows “记事本”创建一个文本文件,其中每行包含一段英文,试读出文件的全部内容,并判断: (1)该文本文件共有多少行? (2)文件中以大写字母P开头的有多少行? (3)一行中包含字符最多的及包含字母...
recommend-type

java记事本的设计与实现

本组课程设计的课题是“记事本的设计与实现”,设计的记事本系统应具有新建、打开、保存、剪切、复制、粘贴、查找、字体设置等基本功能,满足用户对文件的处理,文本文档的编辑和字体的设置等要求。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。