编写代码用Java制作基本的运算器的GUI界面
时间: 2024-05-17 19:14:52 浏览: 12
好的,以下是一个简单的Java程序,用于创建一个基本的运算器GUI界面:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
private Double result;
private String operator = "";
private boolean start = true;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
add(display, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
add(panel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label.equals("C")) {
display.setText("0");
result = null;
operator = "";
start = true;
return;
}
if (start) {
if (label.equals("-")) {
display.setText("-");
start = false;
} else {
display.setText(label);
start = false;
}
} else {
if (label.equals(".")) {
if (display.getText().indexOf(".") == -1) {
display.setText(display.getText() + ".");
}
} else if (label.equals("=")) {
if (!operator.equals("")) {
Double secondOperand = Double.parseDouble(display.getText());
if (operator.equals("+")) {
result += secondOperand;
} else if (operator.equals("-")) {
result -= secondOperand;
} else if (operator.equals("*")) {
result *= secondOperand;
} else if (operator.equals("/")) {
result /= secondOperand;
}
display.setText(result.toString());
operator = "";
}
} else {
Double operand = Double.parseDouble(display.getText());
if (operator.equals("+")) {
result += operand;
} else if (operator.equals("-")) {
result -= operand;
} else if (operator.equals("*")) {
result *= operand;
} else if (operator.equals("/")) {
result /= operand;
} else {
result = operand;
}
display.setText(result.toString());
operator = label;
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
这个程序使用了Swing库来创建一个GUI界面,包括一个文本框和一些按钮。它还实现了 ActionListener 接口,以便在用户按下某个按钮时执行相应的操作。当用户按下数字按钮时,程序会将其添加到文本框中;当用户按下运算符按钮时,程序会将其存储在 operator 变量中。当用户按下等于号按钮时,程序会根据存储的运算符执行相应的运算,并将结果显示在文本框中。最后,程序使用 main 方法创建一个 Calculator 对象并显示它。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)