利用JAVA编写一个简单的窗口计算器,要求窗口中包含数字按钮和计算符号按钮,通过点击进行运算
时间: 2024-02-06 19:08:59 浏览: 65
用java做简单的计算器
以下是一个简单的Java窗口计算器示例代码,其中包含数字按钮和计算符号按钮,可以通过点击进行运算:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JButton[] digitButtons;
private JButton[] operatorButtons;
private JTextField displayField;
private JPanel buttonPanel;
private String currentOperator = "";
private double currentValue = 0.0;
public Calculator() {
super("Calculator");
digitButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
digitButtons[i] = new JButton("" + i);
}
operatorButtons = new JButton[4];
operatorButtons[0] = new JButton("+");
operatorButtons[1] = new JButton("-");
operatorButtons[2] = new JButton("*");
operatorButtons[3] = new JButton("/");
displayField = new JTextField(20);
displayField.setEditable(false);
displayField.setHorizontalAlignment(JTextField.RIGHT);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttonPanel.add(digitButtons[7]);
buttonPanel.add(digitButtons[8]);
buttonPanel.add(digitButtons[9]);
buttonPanel.add(operatorButtons[0]);
buttonPanel.add(digitButtons[4]);
buttonPanel.add(digitButtons[5]);
buttonPanel.add(digitButtons[6]);
buttonPanel.add(operatorButtons[1]);
buttonPanel.add(digitButtons[1]);
buttonPanel.add(digitButtons[2]);
buttonPanel.add(digitButtons[3]);
buttonPanel.add(operatorButtons[2]);
buttonPanel.add(digitButtons[0]);
buttonPanel.add(new JButton("."));
buttonPanel.add(operatorButtons[3]);
buttonPanel.add(new JButton("="));
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(displayField, BorderLayout.NORTH);
container.add(buttonPanel, BorderLayout.CENTER);
for (int i = 0; i < 10; i++) {
digitButtons[i].addActionListener(this);
}
for (int i = 0; i < 4; i++) {
operatorButtons[i].addActionListener(this);
}
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton) {
JButton button = (JButton) source;
String buttonText = button.getText();
if (buttonText.matches("[0-9]")) {
displayField.setText(displayField.getText() + buttonText);
} else if (buttonText.equals(".")) {
if (!displayField.getText().contains(".")) {
displayField.setText(displayField.getText() + ".");
}
} else if (buttonText.equals("+")) {
currentValue = Double.parseDouble(displayField.getText());
currentOperator = "+";
displayField.setText("");
} else if (buttonText.equals("-")) {
currentValue = Double.parseDouble(displayField.getText());
currentOperator = "-";
displayField.setText("");
} else if (buttonText.equals("*")) {
currentValue = Double.parseDouble(displayField.getText());
currentOperator = "*";
displayField.setText("");
} else if (buttonText.equals("/")) {
currentValue = Double.parseDouble(displayField.getText());
currentOperator = "/";
displayField.setText("");
} else if (buttonText.equals("=")) {
if (currentOperator.equals("+")) {
double result = currentValue + Double.parseDouble(displayField.getText());
displayField.setText("" + result);
} else if (currentOperator.equals("-")) {
double result = currentValue - Double.parseDouble(displayField.getText());
displayField.setText("" + result);
} else if (currentOperator.equals("*")) {
double result = currentValue * Double.parseDouble(displayField.getText());
displayField.setText("" + result);
} else if (currentOperator.equals("/")) {
double result = currentValue / Double.parseDouble(displayField.getText());
displayField.setText("" + result);
}
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个示例应用程序创建了一个具有数字按钮和计算符号按钮的窗口计算器。每次按下数字按钮时,该数字将添加到显示字段中。当按下计算符号按钮时,当前值将存储,并将当前操作符设置为相应的操作符。当按下等于号按钮时,使用存储的当前值执行相应的计算并显示结果。
阅读全文