你能写一段关于上述问题代码吗
时间: 2024-02-19 09:01:21 浏览: 91
当然可以,以下是一个简单的Java计算器GUI代码,包括标准模式和科学模式:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JPanel buttonPanel;
private JButton[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"0", ".", "=", "/"
};
private JButton sciButton;
private boolean sciMode = false;
public Calculator() {
display = new JTextField(20);
display.setEditable(false);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
sciButton = new JButton("Sci");
sciButton.addActionListener(this);
Container contentPane = getContentPane();
contentPane.add(display, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(sciButton, BorderLayout.SOUTH);
pack();
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Sci")) {
sciMode = !sciMode;
if (sciMode) {
buttonLabels = new String[]{
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"0", ".", "=", "/",
"sqrt", "x^2", "log"
};
} else {
buttonLabels = new String[]{
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"0", ".", "=", "/"
};
}
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i].setText(buttonLabels[i]);
}
} else if (command.equals("=")) {
String expression = display.getText();
double result = eval(expression);
display.setText(Double.toString(result));
} else {
display.setText(display.getText() + command);
}
}
public static double eval(String expression) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < expression.length()) ? expression.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < expression.length()) throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(expression.substring(startPos, this.pos));
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
else if (eat('s')) x = Math.sqrt(x); // square root
else if (eat('x')) x = Math.pow(x, 2); // power of 2
else if (eat('l')) x = Math.log(x); // natural logarithm
return x;
}
}.parse();
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculator.setVisible(true);
}
}
```
这段代码创建了一个具有标准模式和科学模式的计算器GUI,用户可以在文本框中输入表达式,并使用按钮计算结果。按钮的文本将根据当前模式动态更改,用户可以使用Sci按钮在标准模式和科学模式之间切换。
阅读全文