用java的图形用户界面写简易计算器
时间: 2023-11-26 20:05:15 浏览: 114
可以使用Java Swing来编写一个简易计算器,下面是一个示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private JButton buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEquals, buttonClear;
private double num1 = 0, num2 = 0, result = 0;
private String operator = "";
public Calculator() {
setTitle("Calculator");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
textField = new JTextField(20);
textField.setEditable(false);
JPanel panelTop = new JPanel();
panelTop.add(textField);
JPanel panelMiddle = new JPanel();
panelMiddle.setLayout(new GridLayout(4, 4, 5, 5));
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonPlus = new JButton("+");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
buttonMinus = new JButton("-");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
buttonMultiply = new JButton("*");
buttonClear = new JButton("C");
button0 = new JButton("0");
buttonEquals = new JButton("=");
buttonDivide = new JButton("/");
panelMiddle.add(button7);
panelMiddle.add(button8);
panelMiddle.add(button9);
panelMiddle.add(buttonPlus);
panelMiddle.add(button4);
panelMiddle.add(button5);
panelMiddle.add(button6);
panelMiddle.add(buttonMinus);
panelMiddle.add(button1);
panelMiddle.add(button2);
panelMiddle.add(button3);
panelMiddle.add(buttonMultiply);
panelMiddle.add(buttonClear);
panelMiddle.add(button0);
panelMiddle.add(buttonEquals);
panelMiddle.add(buttonDivide);
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);
buttonPlus.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEquals.addActionListener(this);
buttonClear.addActionListener(this);
JPanel panelBottom = new JPanel();
panelBottom.add(panelMiddle);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(panelTop, BorderLayout.NORTH);
container.add(panelBottom, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
textField.setText(textField.getText() + "0");
} else if (e.getSource() == button1) {
textField.setText(textField.getText() + "1");
} else if (e.getSource() == button2) {
textField.setText(textField.getText() + "2");
} else if (e.getSource() == button3) {
textField.setText(textField.getText() + "3");
} else if (e.getSource() == button4) {
textField.setText(textField.getText() + "4");
} else if (e.getSource() == button5) {
textField.setText(textField.getText() + "5");
} else if (e.getSource() == button6) {
textField.setText(textField.getText() + "6");
} else if (e.getSource() == button7) {
textField.setText(textField.getText() + "7");
} else if (e.getSource() == button8) {
textField.setText(textField.getText() + "8");
} else if (e.getSource() == button9) {
textField.setText(textField.getText() + "9");
} else if (e.getSource() == buttonClear) {
textField.setText("");
num1 = 0;
num2 = 0;
result = 0;
operator = "";
} else if (e.getSource() == buttonPlus) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (e.getSource() == buttonMinus) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (e.getSource() == buttonMultiply) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (e.getSource() == buttonDivide) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (e.getSource() == buttonEquals) {
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("/")) {
result = num1 / num2;
}
textField.setText("" + result);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器支持加、减、乘、除四种基本运算,以及清空和等于两种操作。用户可以通过点击按钮输入数字和选择运算符,即可得到计算结果。
阅读全文