.编写JFrame程序实现一个计算器,包括十个数字(0-9)按钮和四个运算符(加、减、乘、除)按钮,以及等号和清空两个辅助按钮,还有一个显示输入输出的文本框。程序中使用BorderLayout和GridLayout
时间: 2024-02-27 07:59:13 浏览: 43
以下是 Java 实现一个简单计算器的示例代码:
```java
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSub, buttonMul, buttonDiv, buttonEqu, buttonC;
private double num1 = 0, num2 = 0;
private String operator = "";
public Calculator() {
super("Calculator");
textField = new JTextField(20);
textField.setEditable(false);
button0 = new JButton("0");
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");
buttonAdd = new JButton("+");
buttonSub = new JButton("-");
buttonMul = new JButton("*");
buttonDiv = new JButton("/");
buttonEqu = new JButton("=");
buttonC = new JButton("C");
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 3));
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
panel1.add(button6);
panel1.add(button7);
panel1.add(button8);
panel1.add(button9);
panel1.add(button0);
panel1.add(buttonC);
panel1.add(buttonEqu);
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 1));
panel2.add(buttonAdd);
panel2.add(buttonSub);
panel2.add(buttonMul);
panel2.add(buttonDiv);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(textField, BorderLayout.NORTH);
panel.add(panel1, BorderLayout.CENTER);
panel.add(panel2, BorderLayout.EAST);
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);
buttonAdd.addActionListener(this);
buttonSub.addActionListener(this);
buttonMul.addActionListener(this);
buttonDiv.addActionListener(this);
buttonEqu.addActionListener(this);
buttonC.addActionListener(this);
this.add(panel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if ("0123456789".indexOf(str) != -1) {
textField.setText(textField.getText() + str);
return;
}
if ("+-*/".indexOf(str) != -1) {
num1 = Double.parseDouble(textField.getText());
operator = str;
textField.setText("");
return;
}
if ("=".equals(str)) {
num2 = Double.parseDouble(textField.getText());
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0) {
textField.setText("除数不能为0!");
return;
} else {
result = num1 / num2;
}
break;
default:
break;
}
textField.setText(Double.toString(result));
operator = "";
return;
}
if ("C".equals(str)) {
textField.setText("");
num1 = 0;
num2 = 0;
operator = "";
return;
}
}
}
```
这个程序使用了 BorderLayout 和 GridLayout 布局管理器来布置计算器的按钮,并实现了按钮的事件处理逻辑。用户点击数字按钮时,将数字添加到文本框中;用户点击运算符按钮时,将第一个数值和运算符保存下来,并清空文本框;用户点击等号按钮时,将第二个数值取出来,根据运算符计算结果,并显示在文本框中;用户点击清空按钮时,将所有状态清空。
阅读全文