java创建一个计算器窗口
时间: 2023-08-09 13:05:08 浏览: 54
以下是Java创建一个简单计算器窗口的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField displayField;
private double num1, num2, result;
private int operator;
public Calculator() {
setTitle("Calculator");
setSize(250, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4, 5, 5));
displayField = new JTextField(20);
displayField.setEditable(false);
displayField.setHorizontalAlignment(JTextField.RIGHT);
panel.add(displayField);
JButton btn0 = new JButton("0");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btnAdd = new JButton("+");
JButton btnSub = new JButton("-");
JButton btnMul = new JButton("*");
JButton btnDiv = new JButton("/");
JButton btnEqual = new JButton("=");
JButton btnClear = new JButton("C");
JButton btnDecimal = new JButton(".");
panel.add(btn7);
panel.add(btn8);
panel.add(btn9);
panel.add(btnDiv);
panel.add(btn4);
panel.add(btn5);
panel.add(btn6);
panel.add(btnMul);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btnSub);
panel.add(btn0);
panel.add(btnDecimal);
panel.add(btnClear);
panel.add(btnAdd);
panel.add(btnEqual);
btn0.addActionListener(this);
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);
btnAdd.addActionListener(this);
btnSub.addActionListener(this);
btnMul.addActionListener(this);
btnDiv.addActionListener(this);
btnEqual.addActionListener(this);
btnClear.addActionListener(this);
btnDecimal.addActionListener(this);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String btnText = e.getActionCommand();
if (btnText.equals("C")) {
num1 = 0;
num2 = 0;
result = 0;
operator = 0;
displayField.setText("");
}
else if (btnText.equals("+")) {
num1 = Double.parseDouble(displayField.getText());
operator = 1;
displayField.setText("");
}
else if (btnText.equals("-")) {
num1 = Double.parseDouble(displayField.getText());
operator = 2;
displayField.setText("");
}
else if (btnText.equals("*")) {
num1 = Double.parseDouble(displayField.getText());
operator = 3;
displayField.setText("");
}
else if (btnText.equals("/")) {
num1 = Double.parseDouble(displayField.getText());
operator = 4;
displayField.setText("");
}
else if (btnText.equals("=")) {
num2 = Double.parseDouble(displayField.getText());
switch (operator) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
default:
result = 0;
}
displayField.setText(Double.toString(result));
}
else {
displayField.setText(displayField.getText() + btnText);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
在这个示例中,我们创建了一个继承自JFrame的窗口类Calculator,并实现了ActionListener接口用于监听按钮事件。在构造函数中,我们定义了一个JPanel作为容器,并在其中添加了所有的组件,包括文本框、数字键和操作符键。我们还将每个按钮的ActionCommand设置为其显示的文本,方便在按钮事件中判断。
在actionPerformed方法中,我们根据不同的按钮事件来做出相应的处理。例如,当用户点击数字键时,我们将其显示的数字添加到文本框中;当用户点击操作符键时,我们将当前文本框中的数字作为第一个操作数,并将操作符保存起来;当用户点击等号键时,我们将当前文本框中的数字作为第二个操作数,并根据之前保存的操作符进行计算,最后将结果显示在文本框中。
最后,在main方法中,我们创建了一个Calculator对象,即可显示出计算器窗口。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)