仿照windows记事本,制作一个记事本软件,实现纯文本文件的新建、打开、保存、另存、复制、粘贴、查找、替换、关于等等功能;关闭软件是弹出对话框提示是否把修改保存,根据用户选择更新磁盘文件内容 java直接给出代码

时间: 2023-06-24 18:04:37 浏览: 48
以下是一个简单的Java记事本程序,实现了新建、打开、保存、另存、复制、粘贴、查找、替换、关于等功能: ```java import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import javax.swing.Box; import javax.swing.JButton; 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.KeyStroke; import javax.swing.WindowConstants; public class Notepad extends JFrame implements ActionListener, KeyListener { private static final long serialVersionUID = 1L; private JTextArea textArea; private JLabel statusLabel; private File currentFile; private boolean modified = false; public Notepad() { super("记事本"); Container contentPane = getContentPane(); textArea = new JTextArea(); textArea.setFont(new Font("宋体", Font.PLAIN, 16)); textArea.addKeyListener(this); JScrollPane scrollPane = new JScrollPane(textArea); statusLabel = new JLabel("就绪"); statusLabel.setPreferredSize(new Dimension(200, 20)); contentPane.add(scrollPane, BorderLayout.CENTER); contentPane.add(statusLabel, BorderLayout.SOUTH); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("文件"); JMenuItem newMenuItem = new JMenuItem("新建"); newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK)); newMenuItem.addActionListener(this); fileMenu.add(newMenuItem); JMenuItem openMenuItem = new JMenuItem("打开"); openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK)); openMenuItem.addActionListener(this); fileMenu.add(openMenuItem); JMenuItem saveMenuItem = new JMenuItem("保存"); saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK)); saveMenuItem.addActionListener(this); fileMenu.add(saveMenuItem); JMenuItem saveAsMenuItem = new JMenuItem("另存为"); saveAsMenuItem.addActionListener(this); fileMenu.add(saveAsMenuItem); menuBar.add(fileMenu); JMenu editMenu = new JMenu("编辑"); JMenuItem copyMenuItem = new JMenuItem("复制"); copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK)); copyMenuItem.addActionListener(this); editMenu.add(copyMenuItem); JMenuItem pasteMenuItem = new JMenuItem("粘贴"); pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_DOWN_MASK)); pasteMenuItem.addActionListener(this); editMenu.add(pasteMenuItem); JMenuItem findMenuItem = new JMenuItem("查找"); findMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_DOWN_MASK)); findMenuItem.addActionListener(this); editMenu.add(findMenuItem); JMenuItem replaceMenuItem = new JMenuItem("替换"); replaceMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_DOWN_MASK)); replaceMenuItem.addActionListener(this); editMenu.add(replaceMenuItem); menuBar.add(editMenu); JMenu helpMenu = new JMenu("帮助"); JMenuItem aboutMenuItem = new JMenuItem("关于"); aboutMenuItem.addActionListener(this); helpMenu.add(aboutMenuItem); menuBar.add(Box.createHorizontalGlue()); menuBar.add(helpMenu); setJMenuBar(menuBar); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JButton newButton = new JButton("新建"); newButton.addActionListener(this); buttonPanel.add(newButton); JButton openButton = new JButton("打开"); openButton.addActionListener(this); buttonPanel.add(openButton); JButton saveButton = new JButton("保存"); saveButton.addActionListener(this); buttonPanel.add(saveButton); contentPane.add(buttonPanel, BorderLayout.NORTH); setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitApplication(); } }); pack(); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new Notepad(); } private void exitApplication() { if (modified) { int choice = JOptionPane.showConfirmDialog(this, "是否保存修改?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION); if (choice == JOptionPane.YES_OPTION) { saveFile(); } else if (choice == JOptionPane.CANCEL_OPTION) { return; } } dispose(); System.exit(0); } private void newFile() { if (modified) { int choice = JOptionPane.showConfirmDialog(this, "是否保存修改?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION); if (choice == JOptionPane.YES_OPTION) { saveFile(); } else if (choice == JOptionPane.CANCEL_OPTION) { return; } } textArea.setText(""); setTitle("无标题"); currentFile = null; modified = false; } private void openFile() { if (modified) { int choice = JOptionPane.showConfirmDialog(this, "是否保存修改?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION); if (choice == JOptionPane.YES_OPTION) { saveFile(); } else if (choice == JOptionPane.CANCEL_OPTION) { return; } } JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; StringBuilder buffer = new StringBuilder(); while ((line = reader.readLine()) != null) { buffer.append(line).append("\n"); } reader.close(); textArea.setText(buffer.toString()); setTitle(file.getAbsolutePath()); currentFile = file; modified = false; } catch (IOException e) { JOptionPane.showMessageDialog(this, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } } private void saveFile() { if (currentFile == null) { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) { currentFile = fileChooser.getSelectedFile(); } else { return; } } try { FileWriter writer = new FileWriter(currentFile); writer.write(textArea.getText()); writer.close(); setTitle(currentFile.getAbsolutePath()); modified = false; } catch (IOException e) { JOptionPane.showMessageDialog(this, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } private void saveFileAs() { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) { currentFile = fileChooser.getSelectedFile(); saveFile(); } } private void copyText() { textArea.copy(); } private void pasteText() { textArea.paste(); } private void findText() { String text = JOptionPane.showInputDialog(this, "查找:", "记事本", JOptionPane.PLAIN_MESSAGE); if (text != null) { String content = textArea.getText(); int index = content.indexOf(text); if (index == -1) { JOptionPane.showMessageDialog(this, "未找到指定文本。", "记事本", JOptionPane.INFORMATION_MESSAGE); } else { textArea.select(index, index + text.length()); } } } private void replaceText() { String findText = JOptionPane.showInputDialog(this, "查找:", "记事本", JOptionPane.PLAIN_MESSAGE); if (findText != null) { String replaceText = JOptionPane.showInputDialog(this, "替换为:", "记事本", JOptionPane.PLAIN_MESSAGE); if (replaceText != null) { String content = textArea.getText(); int index = content.indexOf(findText); if (index == -1) { JOptionPane.showMessageDialog(this, "未找到指定文本。", "记事本", JOptionPane.INFORMATION_MESSAGE); } else { textArea.replaceRange(replaceText, index, index + findText.length()); modified = true; } } } } private void about() { JOptionPane.showMessageDialog(this, "记事本\n\n版本:1.0\n开发者:xxx\n日期:2022年1月1日", "关于", JOptionPane.INFORMATION_MESSAGE); } @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); switch (command) { case "新建": case "新建(N)...": case "Ctrl+N": newFile(); break; case "打开...": case "打开(O)...": case "Ctrl+O": openFile(); break; case "保存": case "保存(S)": case "Ctrl+S": saveFile(); break; case "另存为(A)...": saveFileAs(); break; case "复制": case "复制(C)": case "Ctrl+C": copyText(); break; case "粘贴": case "粘贴(P)": case "Ctrl+V": pasteText(); break; case "查找...": case "查找(F)...": case "Ctrl+F": findText(); break; case "替换...": case "替换(R)...": case "Ctrl+H": replaceText(); break; case "关于记事本": about(); break; } } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { modified = true; } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { int length = textArea.getText().length(); int caretPosition = textArea.getCaretPosition(); statusLabel.setText(String.format("第 %d 行,第 %d 列,共 %d 个字符", textArea.getLineOfOffset(caretPosition) + 1, caretPosition - textArea.getLineStartOffset(textArea.getLineOfOffset(caretPosition)) + 1, length)); } } ``` 注意,这只是一个简单的实现,可能存在一些漏洞和不足之处,需要根据实际需求进行改进。

相关推荐

最新推荐

recommend-type

面向对象软件开发技术 基于QT的计算器课程报告

仿照Windows系统的计算器软件,为通用计算器设计界面,开发一款实用的计算器软件。 计算器软件基本功能: 计算器包括双目运算和单目运算功能。双目运算符包含基本的四则运算及次幂(^)功能,单目运算符包含阶乘,...
recommend-type

Java仿百度文库,实现文件在线预览

java仿百度文库、豆丁在线阅读,流程如下:Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)=>FlexPaper浏览,本人用过,很好用
recommend-type

华中科技大学电信专业 课程资料 作业 代码 实验报告-数据结构-内含源码和说明书.zip

华中科技大学电信专业 课程资料 作业 代码 实验报告-数据结构-内含源码和说明书.zip
recommend-type

java 游戏飞翔的小鸟

java 制作游戏 飞翔的小鸟
recommend-type

setuptools-25.3.0.zip

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
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

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

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