java swing计算器
时间: 2023-11-24 18:52:42 浏览: 84
以下是一个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 buttonAdd, buttonSubtract, buttonMultiply, buttonDivide, buttonClear, buttonEquals;
private double num1 = 0, num2 = 0, result = 0;
private char operator;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setLocationRelativeTo(null);
textField = new JTextField(20);
textField.setEditable(false);
add(textField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
button7 = new JButton("7");
button7.addActionListener(this);
buttonPanel.add(button7);
button8 = new JButton("8");
button8.addActionListener(this);
buttonPanel.add(button8);
button9 = new JButton("9");
button9.addActionListener(this);
buttonPanel.add(button9);
buttonDivide = new JButton("/");
buttonDivide.addActionListener(this);
buttonPanel.add(buttonDivide);
button4 = new JButton("4");
button4.addActionListener(this);
buttonPanel.add(button4);
button5 = new JButton("5");
button5.addActionListener(this);
buttonPanel.add(button5);
button6 = new JButton("6");
button6.addActionListener(this);
buttonPanel.add(button6);
buttonMultiply = new JButton("*");
buttonMultiply.addActionListener(this);
buttonPanel.add(buttonMultiply);
button1 = new JButton("1");
button1.addActionListener(this);
buttonPanel.add(button1);
button2 = new JButton("2");
button2.addActionListener(this);
buttonPanel.add(button2);
button3 = new JButton("3");
button3.addActionListener(this);
buttonPanel.add(button3);
buttonSubtract = new JButton("-");
buttonSubtract.addActionListener(this);
buttonPanel.add(buttonSubtract);
button0 = new JButton("0");
button0.addActionListener(this);
buttonPanel.add(button0);
buttonClear = new JButton("C");
buttonClear.addActionListener(this);
buttonPanel.add(buttonClear);
buttonEquals = new JButton("=");
buttonEquals.addActionListener(this);
buttonPanel.add(buttonEquals);
buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
buttonPanel.add(buttonAdd);
add(buttonPanel, 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() == 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() == buttonClear) {
textField.setText("");
} else if (e.getSource() == buttonEquals) {
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(Double.toString(result));
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
阅读全文