java swing实现计算器程序
时间: 2023-12-08 20:04:23 浏览: 222
基于Java+Swing实现计算器
好的,以下是一个简单的 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 inputField;
private JButton button0, button1, button2, button3, button4,
button5, button6, button7, button8, button9, buttonAdd,
buttonSubtract, buttonMultiply, buttonDivide, buttonEqual,
buttonClear;
private double num1, num2;
private int operator;
public Calculator() {
super("Calculator");
// 初始化控件
inputField = new JTextField(20);
inputField.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);
// 添加控件到面板
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(buttonDivide);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(buttonMultiply);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(buttonSubtract);
panel.add(button0);
panel.add(buttonClear);
panel.add(buttonEqual);
panel.add(buttonAdd);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(inputField, BorderLayout.NORTH);
container.add(panel, BorderLayout.CENTER);
setSize(300, 250);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button0) {
inputField.setText(inputField.getText() + "0");
} else if (e.getSource() == button1) {
inputField.setText(inputField.getText() + "1");
} else if (e.getSource() == button2) {
inputField.setText(inputField.getText() + "2");
} else if (e.getSource() == button3) {
inputField.setText(inputField.getText() + "3");
} else if (e.getSource() == button4) {
inputField.setText(inputField.getText() + "4");
} else if (e.getSource() == button5) {
inputField.setText(inputField.getText() + "5");
} else if (e.getSource() == button6) {
inputField.setText(inputField.getText() + "6");
} else if (e.getSource() == button7) {
inputField.setText(inputField.getText() + "7");
} else if (e.getSource() == button8) {
inputField.setText(inputField.getText() + "8");
} else if (e.getSource() == button9) {
inputField.setText(inputField.getText() + "9");
} else if (e.getSource() == buttonAdd) {
num1 = Double.parseDouble(inputField.getText());
operator = 1;
inputField.setText("");
} else if (e.getSource() == buttonSubtract) {
num1 = Double.parseDouble(inputField.getText());
operator = 2;
inputField.setText("");
} else if (e.getSource() == buttonMultiply) {
num1 = Double.parseDouble(inputField.getText());
operator = 3;
inputField.setText("");
} else if (e.getSource() == buttonDivide) {
num1 = Double.parseDouble(inputField.getText());
operator = 4;
inputField.setText("");
} else if (e.getSource() == buttonEqual) {
num2 = Double.parseDouble(inputField.getText());
switch (operator) {
case 1:
inputField.setText(String.valueOf(num1 + num2));
break;
case 2:
inputField.setText(String.valueOf(num1 - num2));
break;
case 3:
inputField.setText(String.valueOf(num1 * num2));
break;
case 4:
inputField.setText(String.valueOf(num1 / num2));
break;
}
} else if (e.getSource() == buttonClear) {
inputField.setText("");
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个程序包含了数字、运算符和清除操作按钮,以及文本框用于显示计算结果。当用户点击数字按钮时,数字将显示在文本框中,当用户点击运算符按钮时,程序将记录第一个数值并保存运算符类型,然后清空文本框,以便用户输入第二个数值。当用户点击“=”按钮时,程序将执行相应的运算并将结果显示在文本框中。当用户点击“C”按钮时,程序将清空文本框中的内容。
阅读全文