在eclipse中用用AWT的布局管理器实现以下任意一个计算器的界面设计并实现计算器的运算功能
时间: 2024-06-08 15:12:25 浏览: 92
好的,以下是一个简单的计算器的界面设计和实现计算器的运算功能的代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.Stack;
public class Calculator extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
private TextField display;
private Button[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"0", ".", "=", "/"
};
public Calculator() {
super("Calculator");
setLayout(new BorderLayout());
display = new TextField();
add(display, BorderLayout.NORTH);
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttons = new Button[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new Button(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
add(buttonPanel, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label.equals("=")) {
String result = evaluate(display.getText());
display.setText(result);
} else {
display.setText(display.getText() + label);
}
}
private String evaluate(String expression) {
Stack<Double> values = new Stack<>();
Stack<Character> operators = new Stack<>();
String[] tokens = expression.split(" ");
for (String token : tokens) {
if (token.matches("[+-]?\\d+(\\.\\d+)?")) { // token is a number
double value = Double.parseDouble(token);
values.push(value);
} else if (token.equals("(")) {
operators.push('(');
} else if (token.equals(")")) {
while (!operators.isEmpty() && operators.peek() != '(') {
double b = values.pop();
double a = values.pop();
char op = operators.pop();
double result = applyOperator(a, b, op);
values.push(result);
}
if (!operators.isEmpty() && operators.peek() == '(') {
operators.pop();
}
} else if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
while (!operators.isEmpty() && hasPrecedence(token.charAt(0), operators.peek())) {
double b = values.pop();
double a = values.pop();
char op = operators.pop();
double result = applyOperator(a, b, op);
values.push(result);
}
operators.push(token.charAt(0));
}
}
while (!operators.isEmpty()) {
double b = values.pop();
double a = values.pop();
char op = operators.pop();
double result = applyOperator(a, b, op);
values.push(result);
}
return String.valueOf(values.pop());
}
private boolean hasPrecedence(char op1, char op2) {
if (op2 == '(' || op2 == ')') {
return false;
}
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
return false;
}
return true;
}
private double applyOperator(double a, double b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
throw new IllegalArgumentException("Invalid operator: " + op);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
运行该代码,将会弹出一个简单的计算器界面。用户可以点击按钮输入数字和运算符,点击等于号进行计算,计算结果将会显示在文本框中。该代码中使用了栈来实现表达式的求值,支持加减乘除和括号的运算。
阅读全文