用java编写一个计算器程序(java编写计算器程序代码)

介绍

计算器是我们在日常生活中经常使用的小工具。现代技术的发展使得计算器也能够被开发成为一款程序,方便我们在电脑上使用。本篇文章将介绍如何用Java编写一个简单的计算器程序。

实现

在Java中,我们可以使用Swing框架来实现用户界面。首先,我们需要创建一个JFrame对象作为窗口容器。接下来,我们可以使用JPanel对象来创建一个包含数字和操作符按钮的界面,使用JTextField对象来创建一个文本框,用于显示计算结果。我们还需要为每个按钮添加一个ActionListener,以便用户可以与它们交互。最后,我们可以使用Java中的计算库来实现计算功能。

代码示例

下面是一个使用Java编写的简单的计算器程序的代码示例:

```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {

用java编写一个计算器程序(java编写计算器程序代码)

private JTextField textField;
private JPanel buttonPanel;
private double operand1, operand2, result;
private String operator;

public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
textField = new JTextField();
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
addButton("7", this);
addButton("8", this);
addButton("9", this);
addButton("/", this);
addButton("4", this);
addButton("5", this);
addButton("6", this);
addButton("*", this);
addButton("1", this);
addButton("2", this);
addButton("3", this);
addButton("-", this);
addButton("0", this);
addButton(".", this);
addButton("=", this);
addButton("+", this);
add(textField, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
}

public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if (Character.isDigit(cmd.charAt(0))) {
textField.setText(textField.getText() + cmd);
} else if (cmd.equals(".")) {
textField.setText(textField.getText() + ".");
} else if (cmd.equals("+")) {
operand1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (cmd.equals("-")) {
operand1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (cmd.equals("*")) {
operand1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (cmd.equals("/")) {
operand1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (cmd.equals("=")) {
operand2 = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = operand1 + operand2;
} else if (operator.equals("-")) {
result = operand1 - operand2;
} else if (operator.equals("*")) {
result = operand1 * operand2;
} else if (operator.equals("/")) {
result = operand1 / operand2;
}
textField.setText("" + result);
}
}

private void addButton(String label, ActionListener listener) {
JButton button = new JButton(label);
button.addActionListener(listener);
buttonPanel.add(button);
}

public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```

上面的代码包含了一个名为`Calculator`的类,它继承了JFrame类并实现了ActionListener接口。在构造函数中,我们创建了一个包括数字和操作符按钮的界面,并为每个按钮添加了ActionListener。在ActionListener中,我们使用Java中的计算库来实现计算功能,并将计算结果显示在文本框中。

总结

本篇文章介绍了如何使用Java编写一个简单的计算器程序,并提供了示例代码供读者参考。编写这样的小程序不仅可以增强我们对Java语言的了解,还有助于提高我们的编程能力。希望本文对读者有所帮助。

本文来自投稿,不代表亲测学习网立场,如若转载,请注明出处:https://www.qince.net/javae8tjava.html

郑重声明:

本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。 若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。

我们不承担任何技术及版权问题,且不对任何资源负法律责任。

如遇到资源无法下载,请点击这里失效报错。失效报错提交后记得查看你的留言信息,24小时之内反馈信息。

如有侵犯您的版权,请给我们私信,我们会尽快处理,并诚恳的向你道歉!

(0)
上一篇 2023年4月24日 下午9:24
下一篇 2023年4月24日 下午9:24

猜你喜欢