设计GUI界面的计算器程序,用户可以通过鼠标依次输入参加计算的数值,进行加、减、乘、除等综合混合运算。 要求本计算器程序共有3个Java源文件。 ComputerPad.java该文件生成的类负责创建计算器的“主窗口”。该类含有main方法,整个程序的入口。 NumberButton.java生成的类负责为主类创建数字按钮对象,使用鼠标单击数字按钮可依次输入参加运算的数值的各位数字。 OperationButton.java生成的类负责为主类创建运算符号按钮对象。使用鼠标单击运算符号按钮可选择所要进行的运算。
时间: 2024-03-19 14:44:32 浏览: 149
以下是一个简单的GUI计算器程序的设计,共包含3个Java源文件:
1. ComputerPad.java
```
import javax.swing.*;
public class ComputerPad {
private JFrame frame;
private JTextField display;
private JPanel numberPanel, operationPanel;
private JButton[] numberButtons, operationButtons;
private String currentInput = "";
private double result = 0;
private String currentOperation = "";
public static void main(String[] args) {
ComputerPad computerPad = new ComputerPad();
computerPad.createUI();
}
public void createUI() {
// 创建主窗口
frame = new JFrame("计算器");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
// 创建数字按钮面板
numberPanel = new JPanel();
numberPanel.setLayout(new GridLayout(4, 3));
numberButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
numberButtons[i] = new NumberButton(i, this);
numberPanel.add(numberButtons[i]);
}
// 创建运算符号按钮面板
operationPanel = new JPanel();
operationPanel.setLayout(new GridLayout(4, 1));
operationButtons = new JButton[4];
operationButtons[0] = new OperationButton("+", this);
operationButtons[1] = new OperationButton("-", this);
operationButtons[2] = new OperationButton("*", this);
operationButtons[3] = new OperationButton("/", this);
for (int i = 0; i < 4; i++) {
operationPanel.add(operationButtons[i]);
}
// 创建显示面板
display = new JTextField();
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
// 组装界面
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(display, BorderLayout.NORTH);
mainPanel.add(numberPanel, BorderLayout.CENTER);
mainPanel.add(operationPanel, BorderLayout.EAST);
frame.add(mainPanel);
frame.setVisible(true);
}
public void appendInput(int number) {
currentInput += String.valueOf(number);
display.setText(currentInput);
}
public void setOperation(String operation) {
if (currentOperation.isEmpty()) {
result = Double.parseDouble(currentInput);
} else {
result = evaluate(result, currentOperation, Double.parseDouble(currentInput));
}
currentOperation = operation;
currentInput = "";
}
public void calculate() {
result = evaluate(result, currentOperation, Double.parseDouble(currentInput));
currentInput = "";
currentOperation = "";
display.setText(String.valueOf(result));
}
private double evaluate(double a, String op, double b) {
switch (op) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
return a / b;
default:
return 0;
}
}
}
```
2. NumberButton.java
```
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NumberButton extends JButton implements ActionListener {
private int number;
private ComputerPad computerPad;
public NumberButton(int number, ComputerPad computerPad) {
this.number = number;
this.computerPad = computerPad;
setText(String.valueOf(number));
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
computerPad.appendInput(number);
}
}
```
3. OperationButton.java
```
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class OperationButton extends JButton implements ActionListener {
private String operation;
private ComputerPad computerPad;
public OperationButton(String operation, ComputerPad computerPad) {
this.operation = operation;
this.computerPad = computerPad;
setText(operation);
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (operation.equals("=")) {
computerPad.calculate();
} else {
computerPad.setOperation(operation);
}
}
}
```
这个计算器程序的界面是一个三行四列的网格布局,其中第一行是显示面板,第二行是数字按钮面板,第三行是运算符号按钮面板。每个数字按钮和运算符号按钮都是一个独立的JButton对象,通过继承JButton并实现ActionListener接口来实现。在ComputerPad类中,我们定义了一些事件处理方法,例如appendInput()方法用来将数字输入到当前的输入串中,setOperation()方法用来设置当前的运算符号,calculate()方法用来进行计算。
阅读全文