图形用户界面设计 设计一个计算器,界面如下图所示。有加、减、乘、除、等号、C、BackSpace,数字0、1、2、3、4、5、6、7、8、9、小数点这些按钮,实现基本计算器功能。点击按钮生成的数据在上面的框中显示,C表示整个数据的清除,Backspace是单个数位的清除。按等号给出四则运算的结果。 对于异常的表达式运算,要给出提示; 2. 对于异常的情况,不能程序中断,比如除数为0,不能程序结束,还要能回归正常状态,继续运行。 java代码实现
时间: 2023-07-10 13:43:01 浏览: 40
好的,下面是一个简单的Java Swing计算器实现,满足你的要求:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField displayField;
private String currentOperator = "";
private double currentValue = 0.0;
private boolean startNewValue = true;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建面板
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
// 添加文本框
displayField = new JTextField("0");
displayField.setHorizontalAlignment(JTextField.RIGHT);
displayField.setEditable(false);
panel.add(displayField);
// 添加按钮
addButton(panel, "C");
addButton(panel, "");
addButton(panel, "");
addButton(panel, "BackSpace");
addButton(panel, "7");
addButton(panel, "8");
addButton(panel, "9");
addButton(panel, "/");
addButton(panel, "4");
addButton(panel, "5");
addButton(panel, "6");
addButton(panel, "*");
addButton(panel, "1");
addButton(panel, "2");
addButton(panel, "3");
addButton(panel, "-");
addButton(panel, "0");
addButton(panel, ".");
addButton(panel, "");
addButton(panel, "+");
addButton(panel, "");
addButton(panel, "");
addButton(panel, "");
addButton(panel, "=");
getContentPane().add(panel, BorderLayout.CENTER);
pack();
}
// 添加按钮
private void addButton(Container panel, String buttonText) {
JButton button = new JButton(buttonText);
button.addActionListener(this);
panel.add(button);
}
// 处理按钮的点击事件
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("C")) {
currentOperator = "";
currentValue = 0.0;
startNewValue = true;
displayField.setText("0");
} else if (command.equals("BackSpace")) {
String currentText = displayField.getText();
if (currentText.length() > 1) {
displayField.setText(currentText.substring(0, currentText.length() - 1));
} else {
displayField.setText("0");
}
} else if (command.equals("+") || command.equals("-") || command.equals("*") || command.equals("/")) {
currentOperator = command;
currentValue = Double.parseDouble(displayField.getText());
startNewValue = true;
} else if (command.equals("=")) {
try {
double secondValue = Double.parseDouble(displayField.getText());
double result = 0.0;
if (currentOperator.equals("+")) {
result = currentValue + secondValue;
} else if (currentOperator.equals("-")) {
result = currentValue - secondValue;
} else if (currentOperator.equals("*")) {
result = currentValue * secondValue;
} else if (currentOperator.equals("/")) {
if (secondValue == 0.0) {
throw new ArithmeticException("除数不能为0");
} else {
result = currentValue / secondValue;
}
}
displayField.setText(Double.toString(result));
} catch (NumberFormatException ex) {
displayField.setText("错误的表达式");
} catch (ArithmeticException ex) {
displayField.setText(ex.getMessage());
}
currentOperator = "";
currentValue = 0.0;
startNewValue = true;
} else {
String currentText = displayField.getText();
if (startNewValue) {
currentText = "";
startNewValue = false;
}
displayField.setText(currentText + command);
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
在这个实现中,我们使用JTextField作为文本框,用于显示用户的输入和计算结果。我们使用JPanel作为容器,用GridLayout布局管理器将数字和操作符排列成网格状。
我们使用ActionListener接口来监听按钮的点击事件,对于不同的按钮,我们实现了不同的处理逻辑。例如,对于加减乘除按钮,我们将当前操作符设置为相应的符号,并将当前值保存下来。对于数字和小数点按钮,我们将它们的值添加到文本框中。对于Backspace按钮,我们删除文本框中最后一个字符。对于等号按钮,我们将文本框中的表达式进行计算,并将结果显示在文本框中。如果发生异常,我们将相应的提示信息显示在文本框中。
希望这个代码能够帮助到你。