import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CalculatorTest test = new CalculatorTest();
CalculatorFrame frame = test.new CalculatorFrame();
frame.setVisible(true);
}
});
}
public class CalculatorFrame extends JFrame {
private JPanel jPanel;
private JButton display;
private CalculatorListener calculatorListener;
private NumberListener numberListener;
private boolean start = true;
private String last = "=";
private double result;
public CalculatorFrame() {
this.setSize(400, 400);
this.setLayout(new BorderLayout());
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationByPlatform(false);
jPanel = new JPanel(new GridLayout(4, 4));
display = new JButton("0");
display.setEnabled(false);
calculatorListener = new CalculatorListener();
numberListener = new NumberListener();
this.add(display, BorderLayout.NORTH);
this.add(jPanel, BorderLayout.CENTER);
addButton("7", numberListener);
addButton("8", numberListener);
addButton("9", numberListener);
addButton("/", calculatorListener);
addButton("4", numberListener);
addButton("5", numberListener);
addButton("6", numberListener);
addButton("*", calculatorListener);
addButton("1", numberListener);
addButton("2", numberListener);
addButton("3", numberListener);
addButton("-", calculatorListener);
addButton("0", numberListener);
addButton(".", numberListener);
addButton("=", calculatorListener);
addButton("+", calculatorListener);
}
private void addButton(String text, ActionListener listener){
JButton jButton = new JButton(text);
jPanel.add(jButton);
jButton.addActionListener(listener);
}
private class CalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (!start) {
String option = e.getActionCommand();
double num = Double.parseDouble(display.getText());
if (last.equals("+")) result += num;
else if (last.equals("-")) result -= num;
else if (last.equals("*")) result *= num;
else if (last.equals("/")) result /= num;
else if (last.equals("=")) result = num;
display.setText(result + "");
last = option;
start = true;
}
}
}
private class NumberListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = e.getActionCommand();
if (start) {
display.setText("");
start = false;
}
display.setText(display.getText() + num);
}
}
}
}
追问有没有如图我的图片那样的,先输入表达式,并且表达式不会消失于屏幕,就想真正计算器那样的。。。我想知道的问题是
比如“3*5+8-9”这样一个字符串(有可能比这个更复杂),我将它转换成真正的数学表达式,并计算出来程序该如何来写???
求解答》》》
追答比如1 + 2 - 3 * 4 / 5 + 6 * ( 7 + 8 ),
设置变量num1,num2,num3,operator1,operator2,
然后num1=1,num2=2,num3=3,operator1='+',operator2='-',
这里判断operator2不是乘或除,所以先算operator1,
然后num1=1+2=3,num2=3,num3=4,operator1='-',operator2='*',
这里判断operator2是乘或除,operator1不是乘或除,所以先算operator2,
然后以此类推,
碰到括号就把括号中的东西取出来递归上述过程
本回答被网友采纳