使用java中的GUI写一个计算机
时间: 2023-11-27 15:51:47 浏览: 35
好的,下面是一个简单的Java GUI计算器:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JPanel panel;
private JTextField textField;
private JButton[] buttons;
private String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
private double num1, num2, result;
private String operator;
public Calculator() {
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
add(panel, BorderLayout.CENTER);
textField = new JTextField();
add(textField, BorderLayout.NORTH);
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
panel.add(buttons[i]);
buttons[i].addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
String buttonText = e.getActionCommand();
if (buttonText.matches("[0-9.]")) {
textField.setText(textField.getText() + buttonText);
} else if (buttonText.matches("[/*\\-+\\=]")) {
if (operator == null) {
num1 = Double.parseDouble(textField.getText());
operator = buttonText;
textField.setText("");
} else {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case "+":
result = num1 + num2;
textField.setText(Double.toString(result));
num1 = result;
break;
case "-":
result = num1 - num2;
textField.setText(Double.toString(result));
num1 = result;
break;
case "*":
result = num1 * num2;
textField.setText(Double.toString(result));
num1 = result;
break;
case "/":
result = num1 / num2;
textField.setText(Double.toString(result));
num1 = result;
break;
}
operator = buttonText;
if (buttonText.equals("=")) {
operator = null;
}
textField.setText("");
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个计算器使用了JFrame作为窗口,JPanel作为按钮的容器,并使用了GridLayout布局来排列按钮。当用户点击按钮时,程序会根据输入的数字和运算符计算结果并显示在文本框中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)