使用 java 中的键绑定等待按键

Wait for key pressed using key bindings in java

我是一个用 Swing Java 编写问答游戏的初学者,我希望 user/s 通过按键来回答问题。 我无法让程序等到按下某个键。


我试过调用该方法,然后用一个 Getter 方法的 while 循环让 answer1 “暂停”程序,直到变量是我想要的答案,但当我这样做时程序就冻结了那。


我搜索了很多,但没有一个是有效的。 这是代码示例:

public static void addKeyBinding(int keyCode, String id, ActionListener lambda){
    InputMap im = GUI().getInputMap(WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = GUI().getActionMap();
    im.put(KeyStroke.getKeyStroke(keyCode, 0 ,false), id);
    am.put(id, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            lambda.actionPerformed(e);
        }
    });
}


public char keyOperationSinglePlayer(){
    addKeyBinding(KeyEvent.VK_A, "A", (evt) -> {
        answer1 = 'a';
    });

    addKeyBinding(KeyEvent.VK_S, "S", (evt) -> {
        answer1 = 'b';
    });
    addKeyBinding(KeyEvent.VK_D, "D", (evt) -> {
        answer1 = 'c';
    });
    addKeyBinding(KeyEvent.VK_F, "F", (evt) -> {
        answer1 = 'd';
    });
    return answer1;
}`

谢谢

public class GUI extends JPanel

嗯,已经一年了,我偶然发现了我的旧项目,并决定让任何有兴趣的人 post 我最终做了什么。

 /**
 * Assigns keys and then invokes the methods of "answer processing" logic
 *
 * @throws IOException , exception
 */
public void keyAssignment() throws IOException {
    //key assignment
    CorrectAnswer ca = CorrectAnswer.CorrectAnswer();
    Betting b = Betting.Betting();
    Questions questions = Questions.Question();
    Rounds rounds = Rounds.Rounds();
    //reset keys
    answer1 = ' ';
    answer2 = ' ';
    game3Panel.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            //super.keyTyped(e);
            char temp;
            switch (e.getKeyChar()){
                case 'a' :
                    //a for player 1
                    answer1 = 'a';
                    delUpdatedScreen();
                    temp = questions.getCorrectLetter();
                    if(rounds.getRound().equals("ca")){
                        ca.setPoints(resultPane(answer1, temp), 1);
                    }
                    if(rounds.getRound().equals("b")){
                        b.setPoints(resultPane(answer1, temp), 1);
                    }
                    break;
                case 's' :
                    //b for player 1
                    answer1 = 'b';
                    delUpdatedScreen();
                    temp = questions.getCorrectLetter();
                    if(rounds.getRound().equals("ca")){
                        ca.setPoints(resultPane(answer1, temp), 1);
                    }
                    if(rounds.getRound().equals("b")){
                        b.setPoints(resultPane(answer1, temp), 1);
                    }
                    break;
                case 'd' :
                    //c for player 1
                    answer1 = 'c';
                    delUpdatedScreen();
                    temp = questions.getCorrectLetter();
                    if(rounds.getRound().equals("ca")){
                        ca.setPoints(resultPane(answer1, temp), 1);
                    }
                    if(rounds.getRound().equals("b")){
                        b.setPoints(resultPane(answer1, temp), 1);
                    }
                    break;
                case 'f' :
                    //d for player 1
                    answer1 = 'd';
                    delUpdatedScreen();
                    temp = questions.getCorrectLetter();
                    if(rounds.getRound().equals("ca")){
                        ca.setPoints(resultPane(answer1, temp), 1);
                    }
                    if(rounds.getRound().equals("b")){
                        b.setPoints(resultPane(answer1, temp), 1);
                    }
                    break;
                case 'h' :
                    if(players == 2){
                        //a for player 2
                        answer2 = 'a';
                        delUpdatedScreen();
                        temp = questions.getCorrectLetter();
                        if(rounds.getRound().equals("ca")){
                            ca.setPoints(resultPane(answer2, temp), 2);
                        }
                        if(rounds.getRound().equals("b")){
                            b.setPoints(resultPane(answer2, temp), 2);
                        }
                        break;
                    }
                case 'j' :
                    if(players == 2){
                        //b for player 2
                        answer2 = 'b';
                        delUpdatedScreen();
                        temp = questions.getCorrectLetter();
                        if(rounds.getRound().equals("ca")){
                            ca.setPoints(resultPane(answer2, temp), 2);
                        }
                        if(rounds.getRound().equals("b")){
                            b.setPoints(resultPane(answer2, temp), 2);
                        }
                        break;
                    }
                case 'k' :
                    if(players == 2){
                        //c for player 2
                        answer2 = 'c';
                        delUpdatedScreen();
                        temp = questions.getCorrectLetter();
                        if(rounds.getRound().equals("ca")){
                            ca.setPoints(resultPane(answer2, temp), 2);
                        }
                        if(rounds.getRound().equals("b")){
                            b.setPoints(resultPane(answer2, temp), 2);
                        }
                        break;
                    }
                case 'l' :
                    if (players == 2){
                        //d for player 2
                        answer2 = 'd';
                        delUpdatedScreen();
                        temp = questions.getCorrectLetter();
                        if(rounds.getRound().equals("ca")){
                            ca.setPoints(resultPane(answer2, temp), 2);
                        }
                        if(rounds.getRound().equals("b")){
                            b.setPoints(resultPane(answer2, temp), 2);
                        }
                        break;
                    }
            }
        }
    });
}

这是调用它的代码片段。

            game3Panel = new JPanel();
            game3Panel.setOpaque(false);
            game3Panel.setVisible(false);
            game3Panel.setBackground(Color.WHITE);
            BoxLayout boxlayout = new BoxLayout(game3Panel, BoxLayout.Y_AXIS);
            game3Panel.setLayout(boxlayout);

            picLabel = new JLabel();
            picLabel.setIcon(null);
            game3Panel.add(picLabel, BorderLayout.CENTER);

            cat = new JLabel("");
            game3Panel.add(cat);

            question = new JLabel("");
            game3Panel.add(question);

            ans1 = new JLabel("");
            game3Panel.add(ans1);

            ans2 = new JLabel("");
            game3Panel.add(ans2);

            ans3 = new JLabel("");
            game3Panel.add(ans3);

            ans4 = new JLabel("");
            game3Panel.add(ans4);

            frame.setVisible(true);

            frame.setFocusable(false);
            try {
                keyAssignment();
            } catch (IOException e) {
                e.printStackTrace();
            }

这个不是这个问题的最佳解决方案,它有点仓促,但我是编程新手,急于在截止日期前完成,所以如果你有更好的解决方案,或者想要对代码进行更多说明,请随时告诉我! :)