Java中的计算器应用程序 - 未知变量

时间:2016-01-31 22:41:00

标签: java swing

我正在尝试用Java制作简单的计算器。我想为所有操作按钮编写一个ActionListener,例如+=。当我尝试编译它时,我的开关无法看到变量lastOperation。我认识到,在第一个if变量变量alastOperation没有变化(或第二个变体没有看到它)。有人可以帮我解释这个代码并解释一下,为什么它有一个地方?我不明白这个片段,我认为这个变量会改变。

public class Starter {

    public Starter(){
        EventQueue.invokeLater(new Runnable (){
            @Override
            public void run () {
                new MainFrame();
            }
        });
    }

    public static void main(String[] args) {
        new Starter();
    }
}

public class MainFrame extends JFrame {
    private JEditorPane editorPane = new JEditorPane();
    private String[] tab = {"+","-", "/", "*"};

    public MainFrame() {
        super("Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        setSize(350, 400);
        setLayout (new GridLayout(6,4,3,3));

        for (int i = 0; i < 10; i++) {
            JButton digitButton = new JButton(String.valueOf(i));
            add(digitButton);
            digitButton.addActionListener(new DigitListener(editorPane));
        }

        for (int i = 0; i < tab.length; i++) {
            JButton operationButton = new JButton(String.valueOf(tab[i]));
            add(operationButton);
            operationButton.addActionListener(new OperationsListener(editorPane));
        }


        JButton comaButton = new JButton(".");
        add(comaButton);
        comaButton.addActionListener(new DigitListener(editorPane));

        JButton resultButton = new JButton("=");
        add(resultButton);
        resultButton.addActionListener(new OperationsListener(editorPane));

        JButton clearButton = new JButton("C");
        add(clearButton);
        clearButton.addActionListener(new OperationsListener(editorPane));


        add(editorPane);
        editorPane.setEditable(false);
    }   
}

public class DigitListener implements ActionListener {
    private JEditorPane editorPane;
    public DigitListener(JEditorPane editorPane) {
        this.editorPane = editorPane;
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        JButton digitButton = (JButton)e.getSource();
        String button = digitButton.getText();
        editorPane.setText(editorPane.getText() + button);
        editorPane.validate();
    }
}

public class OperationsListener implements ActionListener {
    private JEditorPane editorPane;
    private double a, b, result = 0;
    private String lastOperation;

    public OperationsListener(JEditorPane editorPane) {
        this.editorPane = editorPane;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)e.getSource();
        String operation = button.getText();
        if (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
            a = Double.valueOf(editorPane.getText());
            editorPane.setText("");
            editorPane.validate();
            lastOperation = operation; 
        }

        else if (operation == "=") {
            b = Double.valueOf(editorPane.getText());
            switch (lastOperation) {
                case "+" : result = a + b; break;
                case "-" : result = a - b; break;
                case "*" : result = a * b; break;
                case "/" : result = a / b; break;
            }
            editorPane.setText(String.valueOf(result));
            editorPane.validate();
        }

        else {
            editorPane.setText("");
            editorPane.validate();
        }
    }    
}

0 个答案:

没有答案