编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),3个加减除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
时间: 2024-05-07 12:20:26 浏览: 115
以下是一个使用Java Swing库编写的模拟计算器程序的示例代码,包括面板和网格布局,文本框,数字和操作符按钮以及事件处理程序。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton[] numberButtons = new JButton[10];
private JButton[] operationButtons = new JButton[4];
private JButton clearButton, equalsButton;
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, 400);
textField = new JTextField();
textField.setPreferredSize(new Dimension(250, 50));
textField.setEditable(false);
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
for(int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
}
operationButtons[0] = new JButton("+");
operationButtons[1] = new JButton("-");
operationButtons[2] = new JButton("*");
operationButtons[3] = new JButton("/");
for(int i = 0; i < 4; i++) {
operationButtons[i].addActionListener(this);
}
clearButton = new JButton("C");
clearButton.addActionListener(this);
equalsButton = new JButton("=");
equalsButton.addActionListener(this);
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(operationButtons[0]);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(operationButtons[1]);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(operationButtons[2]);
panel.add(clearButton);
panel.add(numberButtons[0]);
panel.add(equalsButton);
panel.add(operationButtons[3]);
add(textField, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
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() == operationButtons[0]) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
}
if(e.getSource() == operationButtons[1]) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
}
if(e.getSource() == operationButtons[2]) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
}
if(e.getSource() == operationButtons[3]) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
}
if(e.getSource() == equalsButton) {
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) {
Calculator calculator = new Calculator();
}
}
```
阅读全文