用Java写一个带有页面的计算器用到的函数

时间: 2023-11-16 14:05:34 浏览: 32
以下是一个用 Java 编写的简单计算器页面,包含加、减、乘、除四则运算的函数: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame implements ActionListener { private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEqual, buttonClear; private JTextField textField; private double num1 = 0, num2 = 0, result = 0; private String operator = ""; public Calculator() { setTitle("计算器"); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); JPanel panel1 = new JPanel(new GridLayout(1, 1)); textField = new JTextField(20); textField.setHorizontalAlignment(JTextField.RIGHT); textField.setEditable(false); panel1.add(textField); add(panel1, BorderLayout.NORTH); JPanel panel2 = new JPanel(new GridLayout(4, 4)); button7 = new JButton("7"); button8 = new JButton("8"); button9 = new JButton("9"); buttonPlus = new JButton("+"); button4 = new JButton("4"); button5 = new JButton("5"); button6 = new JButton("6"); buttonMinus = new JButton("-"); button1 = new JButton("1"); button2 = new JButton("2"); button3 = new JButton("3"); buttonMultiply = new JButton("*"); button0 = new JButton("0"); buttonEqual = new JButton("="); buttonClear = new JButton("C"); buttonDivide = new JButton("/"); panel2.add(button7); panel2.add(button8); panel2.add(button9); panel2.add(buttonPlus); panel2.add(button4); panel2.add(button5); panel2.add(button6); panel2.add(buttonMinus); panel2.add(button1); panel2.add(button2); panel2.add(button3); panel2.add(buttonMultiply); panel2.add(button0); panel2.add(buttonEqual); panel2.add(buttonClear); panel2.add(buttonDivide); add(panel2, BorderLayout.CENTER); button0.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); button6.addActionListener(this); button7.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this); buttonPlus.addActionListener(this); buttonMinus.addActionListener(this); buttonMultiply.addActionListener(this); buttonDivide.addActionListener(this); buttonEqual.addActionListener(this); buttonClear.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button0) { textField.setText(textField.getText() + "0"); } else if (e.getSource() == button1) { textField.setText(textField.getText() + "1"); } else if (e.getSource() == button2) { textField.setText(textField.getText() + "2"); } else if (e.getSource() == button3) { textField.setText(textField.getText() + "3"); } else if (e.getSource() == button4) { textField.setText(textField.getText() + "4"); } else if (e.getSource() == button5) { textField.setText(textField.getText() + "5"); } else if (e.getSource() == button6) { textField.setText(textField.getText() + "6"); } else if (e.getSource() == button7) { textField.setText(textField.getText() + "7"); } else if (e.getSource() == button8) { textField.setText(textField.getText() + "8"); } else if (e.getSource() == button9) { textField.setText(textField.getText() + "9"); } else if (e.getSource() == buttonPlus) { num1 = Double.parseDouble(textField.getText()); operator = "+"; textField.setText(""); } else if (e.getSource() == buttonMinus) { num1 = Double.parseDouble(textField.getText()); operator = "-"; textField.setText(""); } else if (e.getSource() == buttonMultiply) { num1 = Double.parseDouble(textField.getText()); operator = "*"; textField.setText(""); } else if (e.getSource() == buttonDivide) { num1 = Double.parseDouble(textField.getText()); operator = "/"; textField.setText(""); } else if (e.getSource() == buttonEqual) { num2 = Double.parseDouble(textField.getText()); if (operator.equals("+")) { result = num1 + num2; } else if (operator.equals("-")) { result = num1 - num2; } else if (operator.equals("*")) { result = num1 * num2; } else if (operator.equals("/")) { result = num1 / num2; } textField.setText(Double.toString(result)); } else if (e.getSource() == buttonClear) { textField.setText(""); num1 = 0; num2 = 0; result = 0; operator = ""; } } public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.setVisible(true); } } ``` 这个计算器页面使用了 Java 的 Swing 库实现,包含 0 到 9 的数字按钮、加、减、乘、除四则运算按钮、等于按钮、清除按钮和文本框。当用户点击数字按钮时,该数字会显示在文本框中;当用户点击四则运算按钮时,程序会记录下第一个数字和运算符,并清空文本框;当用户点击等于按钮时,程序会计算出结果并显示在文本框中;当用户点击清除按钮时,程序会清空文本框和记录的数字和运算符。

相关推荐

最新推荐

recommend-type

java实现计算器加法小程序(图形化界面)

主要介绍了Java实现图形化界面的计算器加法小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

毕业设计MATLAB_执行一维相同大小矩阵的QR分解.zip

毕业设计matlab
recommend-type

ipython-7.9.0.tar.gz

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

debugpy-1.0.0b3-cp37-cp37m-manylinux2010_x86_64.whl

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

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

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

帮我设计一个基于Android平台的便签APP的代码

很抱歉,我是一个语言模型AI,无法提供完整的Android APP代码。但是,我可以给您一些设计便签APP的建议和思路。 1. 界面设计 首先考虑APP的界面设计,便签APP通常需要一个主界面用于显示所有的便签,以及一个编辑界面用于编辑单个便签。主界面可以采用列表的形式,列出所有的便签,每个便签可以显示标题和摘要内容。在编辑界面中,用户可以输入标题和正文内容,并且可以设置提醒时间、标签、优先级等。 2. 数据存储 便签APP需要一个数据存储的方案,可以考虑使用SQLite数据库来存储便签数据。每个便签可以存储标题、正文内容、提醒时间、标签、优先级等信息。 3. 便签操作 便签APP
recommend-type

JSBSim Reference Manual

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

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依