用import java.awt.*设计一个简单计算器
时间: 2024-03-27 17:36:44 浏览: 54
用java设计的简单计算器
以下是一个使用 Java AWT 设计的简单计算器的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener {
private TextField resultField;
private double operand1 = 0;
private double operand2 = 0;
private String operator = "";
private boolean isOperatorSelected = false;
public Calculator() {
setTitle("Calculator");
resultField = new TextField("0", 20);
resultField.setEditable(false);
Button btn1 = new Button("1");
Button btn2 = new Button("2");
Button btn3 = new Button("3");
Button btn4 = new Button("4");
Button btn5 = new Button("5");
Button btn6 = new Button("6");
Button btn7 = new Button("7");
Button btn8 = new Button("8");
Button btn9 = new Button("9");
Button btn0 = new Button("0");
Button btnPlus = new Button("+");
Button btnMinus = new Button("-");
Button btnMultiply = new Button("*");
Button btnDivide = new Button("/");
Button btnEquals = new Button("=");
Button btnClear = new Button("C");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn0.addActionListener(this);
btnPlus.addActionListener(this);
btnMinus.addActionListener(this);
btnMultiply.addActionListener(this);
btnDivide.addActionListener(this);
btnEquals.addActionListener(this);
btnClear.addActionListener(this);
Panel buttonPanel = new Panel(new GridLayout(4, 4, 5, 5));
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btnPlus);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btnMinus);
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
buttonPanel.add(btnMultiply);
buttonPanel.add(btnClear);
buttonPanel.add(btn0);
buttonPanel.add(btnEquals);
buttonPanel.add(btnDivide);
setLayout(new BorderLayout(5, 5));
add(resultField, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String buttonText = e.getActionCommand();
switch (buttonText) {
case "C":
operand1 = 0;
operand2 = 0;
operator = "";
isOperatorSelected = false;
resultField.setText("0");
break;
case "+":
case "-":
case "*":
case "/":
if (isOperatorSelected) {
calculate();
}
operator = buttonText;
operand1 = Double.parseDouble(resultField.getText());
isOperatorSelected = true;
break;
case "=":
if (isOperatorSelected) {
calculate();
isOperatorSelected = false;
}
break;
default:
if (isOperatorSelected) {
resultField.setText(buttonText);
operand2 = Double.parseDouble(buttonText);
} else {
resultField.setText(resultField.getText() + buttonText);
operand1 = Double.parseDouble(resultField.getText());
}
break;
}
}
private void calculate() {
operand2 = Double.parseDouble(resultField.getText());
switch (operator) {
case "+":
operand1 += operand2;
break;
case "-":
operand1 -= operand2;
break;
case "*":
operand1 *= operand2;
break;
case "/":
operand1 /= operand2;
break;
}
resultField.setText(Double.toString(operand1));
}
public static void main(String[] args) {
new Calculator();
}
}
```
该计算器界面包含一个文本框和一组按钮,用户可以通过点击按钮来输入数字和运算符,计算器支持加、减、乘、除四种基本运算,以及清除操作和等于操作。在用户输入数字和运算符的过程中,计算器会动态地更新文本框中的内容,用户点击等于操作后,计算器会根据之前的输入进行计算,并将计算结果显示在文本框中。
阅读全文