0

我有一个相当简单的计算器,我正在尝试将键绑定到 JButtons。我是Java新手,我知道的不多。我确实知道它涉及 ActionListener,但我不知道如何将它变成我目前拥有的东西。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class Calculator2 extends JFrame implements ActionListener {

    // Declare the GUI objects and variables HERE
    JTextField ansText;
    JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, plus, minus, multi, div,
            equal, clear;
    JPanel p1, p2, p3;
    Double val1 = 0.0, val2 = 0.0, answer = 0.0;
    int operator = 0;

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

    public Calculator2() {

        // GUI Creation Code goes HERE

        ansText = new JTextField("", 7);
        ansText.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);


        ansText.addKeyListener(new KeyAdapter() { //Allow only numbers in ansText
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
                    e.consume();
                }
            }
        });

        b1 = new JButton("1");
        b1.addActionListener(this);
        b2 = new JButton("2");
        b2.addActionListener(this);
        b3 = new JButton("3");
        b3.addActionListener(this);
        b4 = new JButton("4");
        b4.addActionListener(this);
        b5 = new JButton("5");
        b5.addActionListener(this);
        b6 = new JButton("6");
        b6.addActionListener(this);
        b7 = new JButton("7");
        b7.addActionListener(this);
        b8 = new JButton("8");
        b8.addActionListener(this);
        b9 = new JButton("9");
        b9.addActionListener(this);
        b0 = new JButton("0");
        b0.addActionListener(this);
        plus = new JButton("+");
        plus.addActionListener(this);
        minus = new JButton("-");
        minus.addActionListener(this);
        multi = new JButton("*");
        multi.addActionListener(this);
        div = new JButton("/");
        div.addActionListener(this);
        equal = new JButton("=");
        equal.addActionListener(this);
        clear = new JButton("C");
        clear.addActionListener(this);

        this.setLayout(new BorderLayout());

        p1 = new JPanel();
        this.add(p1, BorderLayout.NORTH);
        p1.setLayout(new GridLayout(0, 1, 2, 2));
        p1.add(ansText);

        p2 = new JPanel();
        this.add(p2, BorderLayout.CENTER);
        p2.setLayout(new GridLayout(4, 3, 2, 2));
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(plus);

        p2.add(b4);
        p2.add(b5);
        p2.add(b6);
        p2.add(minus);

        p2.add(b7);
        p2.add(b8);
        p2.add(b9);
        p2.add(multi);

        p2.add(clear);
        p2.add(b0);
        p2.add(equal);
        p2.add(div);

        this.setSize(200, 250);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        //Number input
        if (e.getSource() == b1) {
            ansText.setText(ansText.getText() + b1.getText());
        } 
        else if (e.getSource() == b2) {
            ansText.setText(ansText.getText() + b2.getText());
        }
        else if (e.getSource() == b3) {
            ansText.setText(ansText.getText() + b3.getText());
        } 
        else if (e.getSource() == b4) {
            ansText.setText(ansText.getText() + b4.getText());
        } 
        else if (e.getSource() == b5) {
            ansText.setText(ansText.getText() + b5.getText());
        } 
        else if (e.getSource() == b6) {
            ansText.setText(ansText.getText() + b6.getText());
        } 
        else if (e.getSource() == b7) {
            ansText.setText(ansText.getText() + b7.getText());
        } 
        else if (e.getSource() == b8) {
            ansText.setText(ansText.getText() + b8.getText());
        } 
        else if (e.getSource() == b9) {
            ansText.setText(ansText.getText() + b9.getText());
        } 
        else if (e.getSource() == b0) {
            ansText.setText(ansText.getText() + b0.getText());
        }

        //Operator input
        else if (e.getSource() == plus) {
            operator = 1;
            val1 = Double.parseDouble(ansText.getText());
            ansText.setText("");
        } 
        else if (e.getSource() == minus) {
            operator = 2;
            val1 = Double.parseDouble(ansText.getText());
            ansText.setText("");
        } 
        else if (e.getSource() == multi) {
            operator = 3;
            val1 = Double.parseDouble(ansText.getText());
            ansText.setText("");
        } 
        else if (e.getSource() == div) {
            operator = 4;
            val1 = Double.parseDouble(ansText.getText());
            ansText.setText("");
        } 
        //Misc
        else if (e.getSource() == equal) {
            val2 = Double.parseDouble(ansText.getText());
            if (operator == 1) {
                answer = val1 + val2;
                ansText.setText("" + answer);
            } else if (operator == 2) {
                answer = val1 - val2;
                ansText.setText("" + answer);
            } else if (operator == 3) {
                answer = val1 * val2;
                ansText.setText("" + answer);
            } else if (operator == 4) {
                answer = val1 / val2;
                ansText.setText("" + answer);
            }
        } 
        else if (e.getSource() == clear) {
            ansText.setText("");
            val1 = 0.0;
            val2 = 0.0;
            answer = 0.0;
            operator = 0;
        }
    }
}
4

2 回答 2

2

如何使用键绑定教程描述了键绑定的详细信息。这是一个简单的示例,如何将操作绑定到小键盘和主键盘上的加号键:

JPanel panel = new JPanel();
JPanel panel = new JPanel();
    panel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
            KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0), "plus");
panel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
        KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS,
                InputEvent.SHIFT_MASK), "plus");

panel.getActionMap().put("plus", plusAction);
panel.add(button);
于 2013-03-22T03:41:33.120 回答
0

你不想让一个类和几个内部类来完成你所有的工作。Java Swing 与大多数其他 GUI 系统一样,基于 MVC,即模型-视图-控制器。

这里的模型是一些包含累加器当前值和存储在显示器中的值的对象。如果您正在编写 RPN 计算器,模型对象将包含 RPN 堆栈。

视图将是 Swing 类:JTextFieldJButtons

Controller 将是一些新的对象,例如CalculatorController,执行实际工作的对象。ActionListener他们不会对数字进行加、减、乘或除,而是调用您的方法,CalculatorController而该对象反过来会更新 JTextField。

jedyobidan 建议查看Oracle 的文档非常好。您希望将击键放入JPanel自身的输入映射中,因为无论用户关注面板中的哪个位置,它们都应该应用。然后,动作映射将AbstractActionsCalculatorController.

我一直认为 Mac 或其前身 NeXT 上的 GUI 开发风格及其 Interface Builder 程序对于新手来说比 Java Swing 使用的风格要好得多。

于 2013-03-22T02:56:11.027 回答