用gava做一个GUI计算器,能实现加减乘除,但是除法如果除数为0会窗口跳出异常
时间: 2024-03-22 10:39:35 浏览: 92
Java实现带有GUI界面的计算器
5星 · 资源好评率100%
好的,那我们可以使用Java Swing库来实现这个GUI计算器。下面是一个简单的实现示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener {
JFrame frame;
JTextField textField;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[9];
JButton addButton, subButton, mulButton, divButton;
JButton decimalButton, equalsButton, deleteButton, clearButton;
JPanel panel;
double num1 = 0, num2 = 0, result = 0;
char operator;
Calculator() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);
textField = new JTextField();
textField.setBounds(50, 25, 300, 50);
textField.setFont(new Font("Arial", Font.PLAIN, 32));
textField.setEditable(false);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decimalButton = new JButton(".");
equalsButton = new JButton("=");
deleteButton = new JButton("Delete");
clearButton = new JButton("Clear");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decimalButton;
functionButtons[5] = equalsButton;
functionButtons[6] = deleteButton;
functionButtons[7] = clearButton;
for (int i = 0; i < 8; i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(new Font("Arial", Font.PLAIN, 18));
}
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(new Font("Arial", Font.PLAIN, 18));
}
deleteButton.setBounds(50, 430, 145, 50);
clearButton.setBounds(205, 430, 145, 50);
panel = new JPanel();
panel.setBounds(50, 100, 300, 300);
panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.setBackground(Color.gray);
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decimalButton);
panel.add(numberButtons[0]);
panel.add(equalsButton);
panel.add(divButton);
frame.add(panel);
frame.add(deleteButton);
frame.add(clearButton);
frame.add(textField);
frame.setVisible(true);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText() + String.valueOf(i));
}
}
if (e.getSource() == decimalButton) {
textField.setText(textField.getText() + ".");
}
if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
}
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
}
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
}
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
}
if (e.getSource() == equalsButton) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
JOptionPane.showMessageDialog(frame, "除数不能为0!");
textField.setText("");
num1 = 0;
num2 = 0;
result = 0;
} else {
result = num1 / num2;
}
break;
}
textField.setText(String.valueOf(result));
num1 = result;
}
if (e.getSource() == clearButton) {
textField.setText("");
num1 = 0;
num2 = 0;
result = 0;
}
if (e.getSource() == deleteButton) {
String string = textField.getText();
textField.setText("");
for (int i = 0; i < string.length() - 1; i++) {
textField.setText(textField.getText() + string.charAt(i));
}
}
}
}
```
这段代码中,我们创建了一个 `Calculator` 类,实现了 `ActionListener` 接口。在 `Calculator` 类的构造函数中,我们创建了一个 `JFrame` 窗口,并添加了一些按钮和文本框,用于实现加减乘除计算器的功能。在 `actionPerformed()` 方法中,我们根据用户点击的按钮来执行相应的操作,包括数字按钮、小数点按钮、加减乘除按钮、等于号按钮、删除按钮和清除按钮。当除数为0时,我们使用 `JOptionPane` 类弹出一个错误消息框告诉用户除数不能为0。
你可以在本地编译和运行这段代码,测试一下这个GUI计算器的功能。
阅读全文