Java计算器(AWT)不能正确计算

我目前正在学校学习Java,其中一项任务是使用AWT构建计算器。使GUI正常工作,但现在我在实现计算过程本身方面很费劲。 事实是,无论操作员按下什么,这个计算器都只会将按下的数字相加,而我不知道如何解决。

如果有人可以给我一个提示或起点,我将非常感激:-)

这是我的代码

import java.awt.*;
import java.awt.event.*;

class CalcPanel extends Panel {

    public double value = 0.0;
    boolean clear = true;
    int operator = '+';
    double mem;

    TextField tf = new TextField("", 5);

    String buttons[]  = {"M+", "7", "8", "9", "/",
            "M-", "4", "5", "6", "*",
            "MR", "1", "2", "3", "-",
            "CE", "0", ".", "=", "+"};

    Button b[] = new Button[buttons.length];

    ActionListener nl = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (clear)tf.setText("");
            clear = false;
            tf.setText(tf.getText()+e.getActionCommand());

        }
    };

    ActionListener cl = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Button pressed = (Button)e.getSource();
            if(pressed == b[15]) {
                tf.setText("");
                clear = true;
            }
        }
    };

    ActionListener op = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // value = Double.parseDouble(tf.getText());
            double zahl = Double.parseDouble(tf.getText());
            String op = String.valueOf(operator);

            switch(operator) {
                case '+':
                    operator = '+';
                    value += zahl;
                    tf.setText(String.valueOf(value));
                    break;
                case '-':
                    operator = '-';
                    value-=zahl;
                    tf.setText(String.valueOf(value));
                    break;
                case '*':
                    operator = '*';
                    value *= zahl;
                    tf.setText(String.valueOf(value));
                    break;
                case '/':
                    operator = '/';
                    value/=zahl;
                    tf.setText(String.valueOf(value));
                    break;
                case '=':
                    tf.setText(""+value);
                    mem = value;
                    value = 0;
                    operator = '+';
                    break;

                default:
                    value = zahl;
                    break;

            }
            // mem = value;
            //  tf.setText(""+value);
            clear = true;
            op = e.getActionCommand();
            // if(op.charAt(0) == '=') {
            // tf.setText(String.valueOf(value));
            //if(operator == -1) {
            //  tf.setText(""+0.0);
            // }else {
            // operator = op.charAt(0);
            // }
            //}
            // op = e.getActionCommand();

        }
    };

    ActionListener ml = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // mem = value;
            Button pressed = (Button)e.getSource();
            if(pressed == b[10]) {
                tf.setText(String.valueOf(mem));

            }
            if(pressed == b[0]) {
                mem += Double.parseDouble(tf.getText());
                tf.setText(String.valueOf(mem));
                mem = value;
            }
            if(pressed == b[5]) {
                mem-=Double.parseDouble(tf.getText());
                tf.setText(String.valueOf(mem));
                mem = value;
            }
        }
    };


    ActionListener pl = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String z = e.getActionCommand();
            if(!(tf.getText().contains("."))) {
                if(z.contains(".")) {
                    tf.setText(tf.getText()+z);
                }
            }
        }
    };

    ActionListener lstnrs[] = {ml, nl, nl, nl, op, 
            ml, nl, nl, nl, op,
            ml, nl, nl, nl, op,
            cl, nl, pl, op, op};

    public CalcPanel() {

        setLayout(new BorderLayout());

        add(tf, BorderLayout.NORTH);
        setFont(new Font("System", Font.PLAIN, 24));

        Panel zahlenPanel1 = new Panel();
        zahlenPanel1.setLayout(new GridLayout(4,5));

        for(int i = 0; i<buttons.length; i++) {
            b[i] = new Button(buttons[i]);
            zahlenPanel1.add(b[i]);

            b[i].addActionListener(lstnrs[i]);

        }
        add(zahlenPanel1, BorderLayout.CENTER);
    }

    public static void main(String args[]) {
        CalcPanel p = new CalcPanel();
        Frame f = new Frame("calculator1");
        f.add(p);

        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        f.setVisible(true);
        f.pack();
    }
}
评论