使用java的gui编写计算器框架
时间: 2024-05-12 11:13:22 浏览: 61
以下是一个简单的 Java GUI 计算器框架的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JLabel label;
private JTextField textField;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEqual, buttonClear;
private double num1 = 0, num2 = 0, result = 0;
private char operator = '+';
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 250);
label = new JLabel("0");
label.setHorizontalAlignment(JLabel.RIGHT);
label.setFont(new Font("Arial", Font.PLAIN, 18));
textField = new JTextField("");
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setFont(new Font("Arial", Font.PLAIN, 18));
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");
buttonPlus = new JButton("+");
buttonMinus = 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);
buttonPlus.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 1));
p1.add(label);
p1.add(textField);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(4, 4));
p2.add(button7);
p2.add(button8);
p2.add(button9);
p2.add(buttonDivide);
p2.add(button4);
p2.add(button5);
p2.add(button6);
p2.add(buttonMultiply);
p2.add(button1);
p2.add(button2);
p2.add(button3);
p2.add(buttonMinus);
p2.add(button0);
p2.add(buttonClear);
p2.add(buttonEqual);
p2.add(buttonPlus);
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if (input.equals("C")) {
num1 = 0;
num2 = 0;
result = 0;
operator = '+';
label.setText("0");
textField.setText("");
} else if (input.equals("+")) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
} else if (input.equals("-")) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
} else if (input.equals("*")) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
} else if (input.equals("/")) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
} else if (input.equals("=")) {
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;
}
label.setText(Double.toString(result));
num1 = 0;
num2 = 0;
operator = '+';
textField.setText("");
} else {
textField.setText(textField.getText() + input);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个代码创建了一个名为“Calculator”的 JFrame 对象,并且添加了一个 JLabel 和一个 JTextField 用于显示结果和输入,以及一组 JButton 用于数字和操作符输入。在 actionPerformed() 方法中,我们对输入进行解析,并且在必要的时候更新计算器的状态。最后,我们使用 main() 方法创建一个 Calculator 对象并显示它。
阅读全文