用Java写一个带图形界面的计算器
时间: 2023-09-12 11:07:50 浏览: 82
好的,以下是一个简单的Java图形界面计算器示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorGUI implements ActionListener {
private JFrame frame;
private JPanel panel;
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.0;
private double num2 = 0.0;
private char operator = ' ';
public CalculatorGUI() {
frame = new JFrame("计算器");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridLayout(5, 4, 5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
textField = new JTextField(20);
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("清空");
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);
panel.add(textField);
panel.add(buttonClear);
panel.add(buttonDivide);
panel.add(buttonMultiply);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttonSubtract);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(buttonAdd);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(buttonEqual);
panel.add(button0);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
if (operator == ' ') {
num1 = num1 * 10 + Double.parseDouble(command);
} else {
num2 = num2 * 10 + Double.parseDouble(command);
}
textField.setText(textField.getText() + command);
break;
case "+":
case "-":
case "*":
case "/":
if (operator == ' ') {
operator = command.charAt(0);
textField.setText(textField.getText() + command);
} else {
calculate();
operator = command.charAt(0);
textField.setText(num1 + command);
num2 = 0.0;
}
break;
case "=":
calculate();
operator = ' ';
textField.setText(Double.toString(num1));
num2 = 0.0;
break;
case "清空":
num1 = 0.0;
num2 = 0.0;
operator = ' ';
textField.setText("");
break;
}
}
private void calculate() {
switch (operator) {
case '+':
num1 += num2;
break;
case '-':
num1 -= num2;
break;
case '*':
num1 *= num2;
break;
case '/':
if (num2 == 0.0) {
JOptionPane.showMessageDialog(frame, "错误:除数不能为0!", "错误", JOptionPane.ERROR_MESSAGE);
return;
} else {
num1 /= num2;
break;
}
}
}
public static void main(String[] args) {
CalculatorGUI calculatorGUI = new CalculatorGUI();
}
}
```
在这个示例中,我们使用了Swing库来创建图形界面,使用了GridLayout布局来排列按钮。每个按钮都有一个ActionListener监听器,当按钮被点击时,将会执行actionPerformed方法。在该方法中,我们根据用户的点击操作进行相应的计算,并更新文本框中的内容。
这只是一个简单的示例代码,您可以根据实际需求进行相应的修改和扩展。
阅读全文