java窗体简易计算器加减乘除
时间: 2023-07-01 13:26:58 浏览: 101
好的,我来为您实现一个Java窗体的简易计算器,支持加减乘除四则运算。请参考以下代码:
```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;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton buttonAdd;
private JButton buttonSubtract;
private JButton buttonMultiply;
private JButton buttonDivide;
private JButton buttonEqual;
private JButton buttonClear;
private JPanel panel;
private double num1 = 0, num2 = 0, result = 0;
private char operator;
public Calculator() {
setTitle("简易计算器");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
textField = new JTextField();
textField.setEditable(false);
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);
panel = new JPanel(new GridLayout(4, 4));
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttonAdd);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(buttonSubtract);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(buttonMultiply);
panel.add(button0);
panel.add(buttonClear);
panel.add(buttonEqual);
panel.add(buttonDivide);
setLayout(new BorderLayout());
add(textField, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
}
@Override
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() == buttonAdd) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
} else if (e.getSource() == buttonSubtract) {
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() == buttonEqual) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
textField.setText("" + result);
} else if (e.getSource() == buttonClear) {
textField.setText("");
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个窗体简易计算器可以通过界面点击数字按钮和运算符按钮输入算式并计算,也可以通过键盘输入数字和运算符进行计算。
阅读全文