Java用swing组件编写一个计算机程序,功能包括加减乘除,开方,百分数,及倒数计算

时间: 2024-03-19 13:43:05 浏览: 13
好的,以下是一个使用Java Swing组件编写的计算器程序,包括加减乘除、开方、百分比和倒数等常用计算功能: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JTextField textField; private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button0; private JButton buttonAdd, buttonSub, buttonMul, buttonDiv, buttonSqrt, buttonPercent, buttonReciprocal; private JButton buttonDot, buttonEqual, buttonClear; private double num1, num2, result; private String operator; public Calculator() { // 设置标题 setTitle("计算器"); // 创建文本框 textField = new JTextField(); textField.setEditable(false); // 创建数字按钮 button1 = new JButton("1"); button2 = new JButton("2"); button3 = new JButton("3"); button4 = new JButton("4"); button5 = new JButton("5"); button6 = new JButton("6"); button7 = new JButton("7"); button8 = new JButton("8"); button9 = new JButton("9"); button0 = new JButton("0"); // 创建运算符按钮 buttonAdd = new JButton("+"); buttonSub = new JButton("-"); buttonMul = new JButton("*"); buttonDiv = new JButton("/"); buttonSqrt = new JButton("√"); buttonPercent = new JButton("%"); buttonReciprocal = new JButton("1/x"); // 创建其他按钮 buttonDot = new JButton("."); buttonEqual = new JButton("="); buttonClear = new JButton("C"); // 设置按钮的字体 Font buttonFont = new Font("宋体", Font.PLAIN, 20); button1.setFont(buttonFont); button2.setFont(buttonFont); button3.setFont(buttonFont); button4.setFont(buttonFont); button5.setFont(buttonFont); button6.setFont(buttonFont); button7.setFont(buttonFont); button8.setFont(buttonFont); button9.setFont(buttonFont); button0.setFont(buttonFont); buttonAdd.setFont(buttonFont); buttonSub.setFont(buttonFont); buttonMul.setFont(buttonFont); buttonDiv.setFont(buttonFont); buttonSqrt.setFont(buttonFont); buttonPercent.setFont(buttonFont); buttonReciprocal.setFont(buttonFont); buttonDot.setFont(buttonFont); buttonEqual.setFont(buttonFont); buttonClear.setFont(buttonFont); // 创建面板 JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JPanel panel4 = new JPanel(); JPanel panel5 = new JPanel(); JPanel panel6 = new JPanel(); // 设置面板的布局 panel1.setLayout(new FlowLayout(FlowLayout.CENTER)); panel2.setLayout(new GridLayout(4, 3, 5, 5)); panel3.setLayout(new GridLayout(1, 2, 5, 5)); panel4.setLayout(new GridLayout(1, 2, 5, 5)); panel5.setLayout(new GridLayout(1, 4, 5, 5)); panel6.setLayout(new GridLayout(1, 2, 5, 5)); // 将组件添加到面板中 panel1.add(textField); panel2.add(button7); panel2.add(button8); panel2.add(button9); panel2.add(button4); panel2.add(button5); panel2.add(button6); panel2.add(button1); panel2.add(button2); panel2.add(button3); panel2.add(button0); panel2.add(buttonDot); panel2.add(buttonClear); panel3.add(buttonSqrt); panel3.add(buttonPercent); panel4.add(buttonReciprocal); panel4.add(buttonDiv); panel5.add(buttonMul); panel5.add(buttonSub); panel5.add(buttonAdd); panel5.add(buttonEqual); panel6.add(buttonClear); // 将面板添加到窗口中 add(panel1, BorderLayout.NORTH); add(panel2, BorderLayout.CENTER); add(panel3, BorderLayout.EAST); add(panel4, BorderLayout.WEST); add(panel5, BorderLayout.SOUTH); add(panel6, BorderLayout.SOUTH); // 为按钮添加事件监听器 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); button0.addActionListener(this); buttonAdd.addActionListener(this); buttonSub.addActionListener(this); buttonMul.addActionListener(this); buttonDiv.addActionListener(this); buttonSqrt.addActionListener(this); buttonPercent.addActionListener(this); buttonReciprocal.addActionListener(this); buttonDot.addActionListener(this); buttonEqual.addActionListener(this); buttonClear.addActionListener(this); // 设置窗口的大小、位置、可见性和关闭方式 setSize(300, 400); setLocationRelativeTo(null); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 处理按钮事件的方法 public void actionPerformed(ActionEvent e) { 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() == button0) { textField.setText(textField.getText() + "0"); } else if (e.getSource() == buttonDot) { if (textField.getText().indexOf(".") == -1) { textField.setText(textField.getText() + "."); } } else if (e.getSource() == buttonClear) { textField.setText(""); } else if (e.getSource() == buttonAdd) { num1 = Double.parseDouble(textField.getText()); operator = "+"; textField.setText(""); } else if (e.getSource() == buttonSub) { num1 = Double.parseDouble(textField.getText()); operator = "-"; textField.setText(""); } else if (e.getSource() == buttonMul) { num1 = Double.parseDouble(textField.getText()); operator = "*"; textField.setText(""); } else if (e.getSource() == buttonDiv) { num1 = Double.parseDouble(textField.getText()); operator = "/"; textField.setText(""); } else if (e.getSource() == buttonSqrt) { num1 = Double.parseDouble(textField.getText()); result = Math.sqrt(num1); textField.setText(Double.toString(result)); } else if (e.getSource() == buttonPercent) { num1 = Double.parseDouble(textField.getText()); result = num1 / 100; textField.setText(Double.toString(result)); } else if (e.getSource() == buttonReciprocal) { num1 = Double.parseDouble(textField.getText()); result = 1 / num1; textField.setText(Double.toString(result)); } 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)); } } public static void main(String[] args) { new Calculator(); } } ``` 这个程序通过创建多个面板,将各个组件添加到不同的面板中,实现了一个简单的计算器界面。在按钮的事件处理方法中,根据用户的点击操作,执行相应的计算,并将结果显示在文本框中。其中,sqrt、% 和 1/x 操作通过 Java 提供的 Math 类实现。

相关推荐

最新推荐

recommend-type

Java Swing 表格组件

表格也是 GUI 中常用的组件,它是一个由多行、多列组成的二维显示区。Swing 的 JTable 以及相关类提供了对表格的支持。使用 JTable 以及相关类,可以创建功能丰富的表格,还可以为表格定义各种显示外观和编辑特性; ...
recommend-type

Java Swing组件编程之JTable表格用法实例详解

主要介绍了Java Swing组件编程之JTable表格用法,结合实例形式详细分析了Swing组件中JTable表格的常见定义与使用方法,需要的朋友可以参考下
recommend-type

java Swing组件setBounds()简单用法实例分析

主要介绍了java Swing组件setBounds()简单用法,结合实例形式分析了Swing组件setBounds()方法的功能与简单使用方法,需要的朋友可以参考下
recommend-type

基于Java swing组件实现简易计算器

主要介绍了基于Java swing组件实现简易计算器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Java Swing组件单选框JRadioButton用法示例

主要介绍了Java Swing组件单选框JRadioButton用法,结合具体实例形式分析了Swing单选框JRadioButton的使用方法及相关操作注意事项,需要的朋友可以参考下
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

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

:YOLO目标检测算法的挑战与机遇:数据质量、计算资源与算法优化,探索未来发展方向

![:YOLO目标检测算法的挑战与机遇:数据质量、计算资源与算法优化,探索未来发展方向](https://img-blog.csdnimg.cn/7e3d12895feb4651b9748135c91e0f1a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5rKJ6YaJ77yM5LqO6aOO5Lit,size_20,color_FFFFFF,t_70,g_se,x_16) # 1. YOLO目标检测算法简介 YOLO(You Only Look Once)是一种
recommend-type

def ObjFun(x,y,beta): # 目标函数 """ Logistic regression loss function :param beta: model parameter vector :param x: feature matrix :param y: label vector :return: loss value """ n = x.shape[0] p = x.shape[1] pred = 1 / (1 + np.exp(-np.dot(x, beta))) pred = np.clip(pred, 1e-15, 1 - 1e-15) # 将预测值限制在一个很小的区间内 ObjVal = -np.sum(y * np.log(pred) + (1 - y) * np.log(1 - pred)) / n return ObjVal

好的,根据上面的目标函数,我们可以推导出其对应的梯度向量函数,如下所示: def gradient(x, y, beta): """ Compute gradient of the logistic regression loss function :param beta: model parameter vector :param x: feature matrix :param y: label vector :return: gradient vector """ n = x.shape[0] pred = 1 /
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。