0

我想要2个按钮:

1) BindBTN - 单击 keyListener/action 时将监听按键并找到该键的 KeyCode。

2) RunBTN - 当点击一个动作时,将等到用户按下相同的键,然后执行 runProgram()

        RunBTN.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                @SuppressWarnings("serial")
                AbstractAction run = new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        runProgram();
                    }
                };

                RunBTN.getInputMap().put(KeyStroke.getKeyStroke("**bound key**"),
                        "run");
                RunBTN.getActionMap().put("run",
                        run);
            }
        });

我应该为 BindBTN 做什么?

4

1 回答 1

0

好的,所以我想通了:

对于 BindBTN:

        BindingBTN.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyPressed(KeyEvent evtBind) 
            {
                BindCmd = evtBind.getKeyCode();
                BindCmdString = KeyEvent.getKeyText(BindCmd);
            }
        });

对于 RunBTN:

        RunBTN.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                @SuppressWarnings("serial")
                AbstractAction run = new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        runProgram();
                    }
                };

                RunBTN.getInputMap().put(KeyStroke.getKeyStroke(BindCmdString),
                        "run");
                RunBTN.getActionMap().put("run",
                        run);
            }
        });
于 2020-03-14T22:12:43.840 回答