用Java语言写一个计算器界面,实现如下计算器界面,并为按钮添加事件处理:当按钮被点击时将对应字符显示到顶部文本框中
时间: 2024-06-09 08:09:34 浏览: 56
这里提供一个使用Java Swing编写的简单计算器界面代码,可以实现基本的加减乘除运算,你可以根据需要进行修改:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField resultText;
private JButton numButton[] = new JButton[10];
private JButton opButton[] = new JButton[6];
private String operator = "";
private double num1 = 0, num2 = 0, result = 0;
public CalculatorUI() {
setTitle("Calculator");
setSize(300, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 创建组件
resultText = new JTextField();
resultText.setEditable(false);
resultText.setHorizontalAlignment(JTextField.RIGHT);
for (int i = 0; i < numButton.length; i++) {
numButton[i] = new JButton(String.valueOf(i));
numButton[i].addActionListener(this);
}
opButton[0] = new JButton("+");
opButton[1] = new JButton("-");
opButton[2] = new JButton("*");
opButton[3] = new JButton("/");
opButton[4] = new JButton("C");
opButton[5] = new JButton("=");
for (int i = 0; i < opButton.length; i++) {
opButton[i].addActionListener(this);
}
// 布局
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
buttonPanel.add(numButton[7]);
buttonPanel.add(numButton[8]);
buttonPanel.add(numButton[9]);
buttonPanel.add(opButton[0]);
buttonPanel.add(numButton[4]);
buttonPanel.add(numButton[5]);
buttonPanel.add(numButton[6]);
buttonPanel.add(opButton[1]);
buttonPanel.add(numButton[1]);
buttonPanel.add(numButton[2]);
buttonPanel.add(numButton[3]);
buttonPanel.add(opButton[2]);
buttonPanel.add(numButton[0]);
buttonPanel.add(opButton[4]);
buttonPanel.add(opButton[5]);
buttonPanel.add(opButton[3]);
// 添加组件
add(resultText, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9') {
resultText.setText(resultText.getText() + command);
} else if (command.equals("+")) {
operator = "+";
num1 = Double.parseDouble(resultText.getText());
resultText.setText("");
} else if (command.equals("-")) {
operator = "-";
num1 = Double.parseDouble(resultText.getText());
resultText.setText("");
} else if (command.equals("*")) {
operator = "*";
num1 = Double.parseDouble(resultText.getText());
resultText.setText("");
} else if (command.equals("/")) {
operator = "/";
num1 = Double.parseDouble(resultText.getText());
resultText.setText("");
} else if (command.equals("C")) {
resultText.setText("");
} else if (command.equals("=")) {
num2 = Double.parseDouble(resultText.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
if (num2 == 0) {
resultText.setText("Error");
return;
} else {
result = num1 / num2;
}
}
resultText.setText(String.valueOf(result));
}
}
public static void main(String[] args) {
CalculatorUI calculator = new CalculatorUI();
calculator.setVisible(true);
}
}
```
运行程序后会弹出一个计算器窗口,可以进行数字和运算符的输入和计算,效果如下:
![CalculatorUI](https://cdn.jsdelivr.net/gh/YukinaMochizuki/ImageHosting//CalculatorUI.png)
阅读全文