用java写编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,要求将计算公式和结果显示在文本框中。
时间: 2024-05-11 09:19:10 浏览: 61
以下是一个简单的Java GUI计算器程序,使用面板和网格布局。它包含一个文本框、10个数字按钮、4个运算符按钮和一个等号按钮。点击按钮后,计算公式和结果将显示在文本框中。
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JButton[] numberButtons = new JButton[10];
private JButton[] operatorButtons = new JButton[4];
private JButton addButton, subButton, mulButton, divButton;
private JButton equalButton, clearButton;
private JPanel panel;
private double num1 = 0, num2 = 0, result = 0;
private char operator;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setResizable(false);
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
equalButton = new JButton("=");
clearButton = new JButton("C");
operatorButtons[0] = addButton;
operatorButtons[1] = subButton;
operatorButtons[2] = mulButton;
operatorButtons[3] = divButton;
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
}
panel = new JPanel(new GridLayout(5, 4));
panel.add(textField);
for (int i = 1; i <= 9; i++) {
if (i == 1) {
panel.add(numberButtons[7]);
} else if (i == 2) {
panel.add(numberButtons[8]);
} else if (i == 3) {
panel.add(numberButtons[9]);
} else if (i == 4) {
panel.add(operatorButtons[0]);
} else if (i == 5) {
panel.add(numberButtons[4]);
} else if (i == 6) {
panel.add(numberButtons[5]);
} else if (i == 7) {
panel.add(numberButtons[6]);
} else if (i == 8) {
panel.add(operatorButtons[1]);
} else if (i == 9) {
panel.add(numberButtons[1]);
} else {
panel.add(numberButtons[0]);
}
}
panel.add(operatorButtons[2]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(operatorButtons[3]);
panel.add(numberButtons[0]);
panel.add(clearButton);
panel.add(equalButton);
for (int i = 0; i < 10; i++) {
numberButtons[i].addActionListener(this);
}
for (int i = 0; i < 4; i++) {
operatorButtons[i].addActionListener(this);
}
equalButton.addActionListener(this);
clearButton.addActionListener(this);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText().concat(String.valueOf(i)));
}
}
if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
}
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
}
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
}
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
}
if (e.getSource() == equalButton) {
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;
}
textField.setText(String.valueOf(result));
num1 = result;
}
if (e.getSource() == clearButton) {
textField.setText("");
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
阅读全文