javaswing界面设计计算器
时间: 2023-09-08 07:11:26 浏览: 104
Java Swing计算器
下面是一个简单的Java Swing界面设计计算器的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 300;
private final int HEIGHT = 400;
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 buttonClear;
private JButton buttonEqual;
private double firstNumber;
private double secondNumber;
private double result;
private String operator;
public Calculator() {
setTitle("Calculator");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
textField = new JTextField(20);
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("/");
buttonClear = new JButton("C");
buttonEqual = new JButton("=");
JPanel panel1 = new JPanel(new GridLayout(1, 1));
panel1.add(textField);
JPanel panel2 = new JPanel(new GridLayout(4, 4));
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
panel2.add(buttonDivide);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.add(buttonMultiply);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(buttonSubtract);
panel2.add(button0);
panel2.add(buttonClear);
panel2.add(buttonEqual);
panel2.add(buttonAdd);
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);
buttonClear.addActionListener(this);
buttonEqual.addActionListener(this);
setLayout(new BorderLayout());
add(panel1, BorderLayout.NORTH);
add(panel2, 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) {
firstNumber = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
} else if (e.getSource() == buttonSubtract) {
firstNumber = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
} else if (e.getSource() == buttonMultiply) {
firstNumber = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
} else if (e.getSource() == buttonDivide) {
firstNumber = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
} else if (e.getSource() == buttonClear) {
textField.setText("");
} else if (e.getSource() == buttonEqual) {
secondNumber = Double.parseDouble(textField.getText());
if (operator.equals("+")) {
result = firstNumber + secondNumber;
} else if (operator.equals("-")) {
result = firstNumber - secondNumber;
} else if (operator.equals("*")) {
result = firstNumber * secondNumber;
} else if (operator.equals("/")) {
result = firstNumber / secondNumber;
}
textField.setText(Double.toString(result));
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个示例代码创建了一个简单的计算器界面,用户可以通过按下数字键和运算符键进行计算。该计算器支持加、减、乘、除四种基本运算,并且提供清除和等于两个特殊功能。
在这个示例代码中,计算器界面被分成了两个JPanel面板。第一个JPanel包含一个JTextField,用于显示用户输入和计算结果。第二个JPanel包含数字键和运算符键。在JFrame的BorderLayout布局中,第一个JPanel被放置在北部,第二个JPanel被放置在中心。
在这个示例代码中,所有数字键和运算符键都被分别创建为JButton对象,并且被添加到第二个JPanel中。每个JButton都有一个ActionListener,当用户单击它时,它将调用actionPerformed()方法。在actionPerformed()方法中,根据单击的按钮类型,执行相应的操作(添加数字、选择运算符、清除文本框、计算结果等)。
最后,该示例代码创建了一个Calculator对象,并将其设置为可见。这将启动计算器应用程序,并显示计算器界面。
阅读全文