使用Java编写一个简易计算器的GUI界面
时间: 2023-05-27 17:06:18 浏览: 115
抱歉,作为AI语言模型,我无法提供完整的Java代码。但是,以下是一个简单的Java计算器GUI界面的示例代码,可以帮助您开始编写自己的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI 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 buttonMinus;
private JButton buttonMultiply;
private JButton buttonDivide;
private JButton buttonEqual;
private JButton buttonClear;
private double num1 = 0;
private double num2 = 0;
private String operator = "";
public CalculatorGUI() {
setTitle("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 400);
setResizable(false);
JPanel panel = new JPanel(new BorderLayout());
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
panel.add(textField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
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("+");
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);
buttonAdd.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonDivide);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonMultiply);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonMinus);
buttonPanel.add(button0);
buttonPanel.add(buttonClear);
buttonPanel.add(buttonEqual);
buttonPanel.add(buttonAdd);
panel.add(buttonPanel, BorderLayout.CENTER);
add(panel);
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() == buttonMinus) {
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());
if (operator.equals("+")) {
textField.setText("" + (num1 + num2));
} else if (operator.equals("-")) {
textField.setText("" + (num1 - num2));
} else if (operator.equals("*")) {
textField.setText("" + (num1 * num2));
} else if (operator.equals("/")) {
textField.setText("" + (num1 / num2));
}
} else if (e.getSource() == buttonClear) {
textField.setText("");
num1 = 0;
num2 = 0;
operator = "";
}
}
public static void main(String[] args) {
new CalculatorGUI();
}
}
```
这个代码创建了一个简单的计算器GUI界面,它包含数字按钮、加减乘除按钮、等号按钮和清除按钮。当用户按下数字按钮时,它们将被添加到文本字段中。当用户按下加减乘除按钮时,程序会记录第一个数字并记录操作符。当用户按下等号按钮时,程序会记录第二个数字并使用之前记录的操作符执行计算。当用户按下清除按钮时,文本字段将被清空,所有数字和操作符变量也将被重置。
阅读全文