计算器界面Java代码
时间: 2023-07-09 10:52:54 浏览: 97
计算器java代码
以下是一个简单的计算器界面Java代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalculatorGUI extends JFrame implements ActionListener {
private JPanel mainPanel;
private JTextField inputField;
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 buttonEquals;
private JButton buttonClear;
private double currentNumber;
private String currentOperator;
private boolean isNewNumber;
public CalculatorGUI() {
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
mainPanel = new JPanel(new BorderLayout());
inputField = new JTextField();
inputField.setHorizontalAlignment(JTextField.RIGHT);
inputField.setPreferredSize(new Dimension(300, 50));
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("+");
buttonSubtract = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEquals = new JButton("=");
buttonClear = new JButton("C");
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonAdd);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonSubtract);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonMultiply);
buttonPanel.add(buttonClear);
buttonPanel.add(button0);
buttonPanel.add(buttonEquals);
buttonPanel.add(buttonDivide);
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);
buttonEquals.addActionListener(this);
buttonClear.addActionListener(this);
currentNumber = 0;
currentOperator = "";
isNewNumber = true;
mainPanel.add(inputField, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
setContentPane(mainPanel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String buttonLabel = e.getActionCommand();
if (buttonLabel.equals("C")) {
inputField.setText("");
currentNumber = 0;
currentOperator = "";
isNewNumber = true;
} else if (buttonLabel.equals("=")) {
double result = 0;
if (currentOperator.equals("+")) {
result = currentNumber + Double.parseDouble(inputField.getText());
} else if (currentOperator.equals("-")) {
result = currentNumber - Double.parseDouble(inputField.getText());
} else if (currentOperator.equals("*")) {
result = currentNumber * Double.parseDouble(inputField.getText());
} else if (currentOperator.equals("/")) {
result = currentNumber / Double.parseDouble(inputField.getText());
}
inputField.setText(String.valueOf(result));
currentNumber = result;
currentOperator = "";
isNewNumber = true;
} else if (buttonLabel.equals("+")) {
currentNumber = Double.parseDouble(inputField.getText());
currentOperator = "+";
isNewNumber = true;
} else if (buttonLabel.equals("-")) {
currentNumber = Double.parseDouble(inputField.getText());
currentOperator = "-";
isNewNumber = true;
} else if (buttonLabel.equals("*")) {
currentNumber = Double.parseDouble(inputField.getText());
currentOperator = "*";
isNewNumber = true;
} else if (buttonLabel.equals("/")) {
currentNumber = Double.parseDouble(inputField.getText());
currentOperator = "/";
isNewNumber = true;
} else {
if (isNewNumber) {
inputField.setText(buttonLabel);
isNewNumber = false;
} else {
inputField.setText(inputField.getText() + buttonLabel);
}
}
}
public static void main(String[] args) {
new CalculatorGUI();
}
}
```
上述代码使用Java Swing GUI库创建了一个简单的计算器界面,并实现了基本的计算功能。用户可以通过按钮输入数字和运算符,计算器会在文本框中显示计算结果。界面还包括一个清除按钮,可以清除当前的计算结果。
阅读全文