用java做一个GUI计算器,能实现加减乘除,但是除法如果除数为0会窗口跳出异常
时间: 2024-03-21 21:38:25 浏览: 92
好的,以下是用 Java 编写的一个简单的 GUI 计算器,可以实现加减乘除的功能。如果除数为 0,会弹出一个对话框提示用户输入错误。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonEqual, buttonClear;
private double num1 = 0, num2 = 0, result = 0;
private String operator = "";
public Calculator() {
setTitle("Calculator");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField(10);
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonAdd = new JButton("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonAdd.addActionListener(this);
buttonSubtract.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonDivide);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonMultiply);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonSubtract);
buttonPanel.add(button0);
buttonPanel.add(buttonEqual);
buttonPanel.add(buttonClear);
buttonPanel.add(buttonAdd);
setLayout(new BorderLayout());
add(textField, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
String buttonText = e.getActionCommand();
if (buttonText.equals("C")) {
textField.setText("");
num1 = 0;
num2 = 0;
result = 0;
operator = "";
} else if (buttonText.equals("+")) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (buttonText.equals("-")) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (buttonText.equals("*")) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (buttonText.equals("/")) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (buttonText.equals("=")) {
num2 = Double.parseDouble(textField.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) {
JOptionPane.showMessageDialog(null, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
textField.setText("");
return;
} else {
result = num1 / num2;
}
}
textField.setText("" + result);
} else {
textField.setText(textField.getText() + buttonText);
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个计算器采用了 Java 的 Swing 框架来实现 GUI,使用了 GridLayout 来布局按钮。当用户点击操作符按钮时,记录下当前的数字和操作符,当用户点击等号按钮时,根据当前的操作符计算结果并显示在文本框中。当用户点击清除按钮时,清空文本框和记录的数字和操作符。当用户点击除法操作符并且除数为 0 时,会弹出一个对话框提示用户输入错误。
阅读全文