用JAVA语言写出1.生成一个简单文本编辑器,将生成的文本存入文件,如果有打开、存盘、关闭等菜单更佳。GUI界面可以自己灵活设计,同时实现窗体关闭退出功能。 2.编程实现一个加减法计算器。可以使用鼠标来完成加减法

时间: 2024-02-01 16:16:14 浏览: 24
1. 简单文本编辑器 以下是一个基于Java Swing库的简单文本编辑器示例代码: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class SimpleTextEditor extends JFrame { private JTextArea textArea; private JFileChooser fileChooser; public SimpleTextEditor() { // 设置窗口标题 super("简单文本编辑器"); // 创建菜单栏 JMenuBar menuBar = new JMenuBar(); // 创建文件菜单 JMenu fileMenu = new JMenu("文件"); JMenuItem openMenuItem = new JMenuItem("打开"); JMenuItem saveMenuItem = new JMenuItem("保存"); JMenuItem exitMenuItem = new JMenuItem("退出"); // 打开文件 openMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int returnVal = fileChooser.showOpenDialog(SimpleTextEditor.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { FileReader reader = new FileReader(file); textArea.read(reader, null); reader.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(SimpleTextEditor.this, "无法打开文件: " + file.getName(), "错误", JOptionPane.ERROR_MESSAGE); } } } }); // 保存文件 saveMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int returnVal = fileChooser.showSaveDialog(SimpleTextEditor.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { FileWriter writer = new FileWriter(file); textArea.write(writer); writer.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(SimpleTextEditor.this, "无法保存文件: " + file.getName(), "错误", JOptionPane.ERROR_MESSAGE); } } } }); // 退出程序 exitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // 添加菜单项到菜单 fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); fileMenu.addSeparator(); fileMenu.add(exitMenuItem); // 添加菜单到菜单栏 menuBar.add(fileMenu); // 创建文本区域 textArea = new JTextArea(); // 创建文件选择器 fileChooser = new JFileChooser(); fileChooser.setFileFilter(new FileFilter() { public boolean accept(File file) { return file.isDirectory() || file.getName().toLowerCase().endsWith(".txt"); } public String getDescription() { return "文本文件 (*.txt)"; } }); // 添加组件到窗口 setJMenuBar(menuBar); add(new JScrollPane(textArea), BorderLayout.CENTER); // 设置窗口属性 setSize(600, 400); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { SimpleTextEditor editor = new SimpleTextEditor(); editor.setVisible(true); } } ``` 2. 加减法计算器 以下是一个基于Java Swing库的加减法计算器示例代码: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame { private JTextField inputField; private JButton addButton; private JButton subtractButton; private JButton clearButton; private int result = 0; public Calculator() { // 设置窗口标题 super("加减法计算器"); // 创建组件 inputField = new JTextField(10); addButton = new JButton("+"); subtractButton = new JButton("-"); clearButton = new JButton("清空"); // 添加事件监听器 addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { int value = Integer.parseInt(inputField.getText()); result += value; inputField.setText(Integer.toString(result)); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(Calculator.this, "请输入一个整数", "错误", JOptionPane.ERROR_MESSAGE); } } }); subtractButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { int value = Integer.parseInt(inputField.getText()); result -= value; inputField.setText(Integer.toString(result)); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(Calculator.this, "请输入一个整数", "错误", JOptionPane.ERROR_MESSAGE); } } }); clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { result = 0; inputField.setText(""); } }); // 创建面板并添加组件 JPanel panel = new JPanel(); panel.add(inputField); panel.add(addButton); panel.add(subtractButton); panel.add(clearButton); // 添加面板到窗口 add(panel); // 设置窗口属性 pack(); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.setVisible(true); } } ```

相关推荐

最新推荐

recommend-type

C++从文本文件读取数据到vector中的方法

主要给大家介绍了利用C++如何从文本文件读取数据到vector中,文章通过实例给出示例代码,相信会对大家的理解和学习很有帮助,有需要的朋友们下面来一起看看吧。
recommend-type

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

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

新建文本文档.txt

新建文本文档
recommend-type

开源Git gui工具Fork

开源Git gui工具Fork,CSDN能找到教程,但是资料不多,推荐用Tortoise
recommend-type

yolov5在华为昇腾atlas上加速推理

该资源为yolov5在华为昇腾atlas上使用Ascend310芯片加速推理,属于c++后端开发,适合C++开发者在华为昇腾盒子上移植深度学习算法的博主们。 资源是demo形式,包含完整的一套代码,还有转好的离线模型文件和跑出的测试结果图片。
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

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

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