java - 将按键绑定(bind)到 JButton

标签 java swing jbutton calculator key-bindings

我有一个相当简单的计算器,我正在尝试将按键绑定(bind)到 JButton。我是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;
        }
    }
}

最佳答案

How to Use Key Bindings教程描述了按键绑定(bind)的详细信息。以下是如何将操作绑定(bind)到小键盘和主键盘上的加号键的简单示例:

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);

关于java - 将按键绑定(bind)到 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15561732/

相关文章:

java - 如何为 RESTful 端点禁用基于 Spring 表单的登录?

java - Osgi swing 嵌入应用程序

java - 如何在鼠标按下事件之前发送焦点丢失事件?

Java JButton - MouseMotionListener ( MouseMoved ) 鼠标悬停效果

java - JButtons 直到悬停在面板上才会加载?

java - 传递给另一个类时清空数组列表

java.util.logging.publish 没有被调用

java - 如何让我的程序在 Java 窗口中运行?

java - 如何将 BorderLayout 与 Netbeans Matisse 一起使用

java - 获取 jbutton 上 imageIcon 的描述