请用java实现一个计算器
时间: 2023-12-12 18:24:27 浏览: 62
用java实现计算器
以下是一个简单的 Java 计算器实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEqual, buttonClear;
private double currentValue = 0;
private String currentOperator = "";
public Calculator() {
super("Calculator");
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField();
textField.setHorizontalAlignment(JTextField.RIGHT);
add(textField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
button7 = new JButton("7");
button7.addActionListener(this);
buttonPanel.add(button7);
button8 = new JButton("8");
button8.addActionListener(this);
buttonPanel.add(button8);
button9 = new JButton("9");
button9.addActionListener(this);
buttonPanel.add(button9);
buttonDivide = new JButton("/");
buttonDivide.addActionListener(this);
buttonPanel.add(buttonDivide);
button4 = new JButton("4");
button4.addActionListener(this);
buttonPanel.add(button4);
button5 = new JButton("5");
button5.addActionListener(this);
buttonPanel.add(button5);
button6 = new JButton("6");
button6.addActionListener(this);
buttonPanel.add(button6);
buttonMultiply = new JButton("*");
buttonMultiply.addActionListener(this);
buttonPanel.add(buttonMultiply);
button1 = new JButton("1");
button1.addActionListener(this);
buttonPanel.add(button1);
button2 = new JButton("2");
button2.addActionListener(this);
buttonPanel.add(button2);
button3 = new JButton("3");
button3.addActionListener(this);
buttonPanel.add(button3);
buttonSubtract = new JButton("-");
buttonSubtract.addActionListener(this);
buttonPanel.add(buttonSubtract);
button0 = new JButton("0");
button0.addActionListener(this);
buttonPanel.add(button0);
buttonClear = new JButton("C");
buttonClear.addActionListener(this);
buttonPanel.add(buttonClear);
buttonEqual = new JButton("=");
buttonEqual.addActionListener(this);
buttonPanel.add(buttonEqual);
buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
buttonPanel.add(buttonAdd);
add(buttonPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String buttonText = e.getActionCommand();
if (buttonText.equals("C")) {
currentValue = 0;
currentOperator = "";
textField.setText("");
return;
}
if (buttonText.equals("+") || buttonText.equals("-") || buttonText.equals("*") || buttonText.equals("/")) {
currentValue = Double.parseDouble(textField.getText());
currentOperator = buttonText;
textField.setText("");
return;
}
if (buttonText.equals("=")) {
double newValue = Double.parseDouble(textField.getText());
if (currentOperator.equals("+")) {
currentValue += newValue;
}
else if (currentOperator.equals("-")) {
currentValue -= newValue;
}
else if (currentOperator.equals("*")) {
currentValue *= newValue;
}
else if (currentOperator.equals("/")) {
currentValue /= newValue;
}
currentOperator = "";
textField.setText(Double.toString(currentValue));
return;
}
String currentText = textField.getText();
textField.setText(currentText + buttonText);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
该计算器使用了 JFrame 类来创建一个窗口界面,并使用了 JTextField 和 JButton 等组件来实现用户界面。用户可以通过单击按钮来输入数字和操作符,并通过单击“C”按钮来清除计算器的状态。在用户单击“=”按钮时,计算器将执行相应的数学运算,并将结果显示在文本框中。
阅读全文