Java 关注多个JPanel的问题&;线

Java 关注多个JPanel的问题&;线,java,swing,jpanel,focus,Java,Swing,Jpanel,Focus,我正在设计一个经典的蛇游戏,在这个游戏中,用户选择一个难度等级(通过JRadioButtons)并使用箭头键控制蛇。我有两个JPanel:SetupPanel和SnakePanel,它们被添加到JFrame和GameFrame中。我正在用一根线使蛇移动 现在,我正在尝试向JRadioButtons添加功能,使速度随着难度的增加而加快。蛇运行良好,直到我选择一个新的难度(在设置面板上)。然后,蛇继续在蛇面板中移动,但不能再使用箭头键移动蛇 我很确定这是一个焦点问题,我花了数小时阅读教程,但似乎没有

我正在设计一个经典的蛇游戏,在这个游戏中,用户选择一个难度等级(通过JRadioButtons)并使用箭头键控制蛇。我有两个JPanel:SetupPanel和SnakePanel,它们被添加到JFrame和GameFrame中。我正在用一根线使蛇移动

现在,我正在尝试向JRadioButtons添加功能,使速度随着难度的增加而加快。蛇运行良好,直到我选择一个新的难度(在设置面板上)。然后,蛇继续在蛇面板中移动,但不能再使用箭头键移动蛇

我很确定这是一个焦点问题,我花了数小时阅读教程,但似乎没有任何帮助

public class GameFrame extends JFrame{

    this.add(new SetupPanel(), BorderLayout.NORTH);

    SnakePanel snakePanel = new SnakePanel();
    this.add(snakePanel, BorderLayout.SOUTH);

    setLocationRelativeTo(null);

    setVisible(true);
    snakePanel.requestFocusInWindow();  //without this my thread doesn't work
}




public class SetupPanel extends JPanel{

    JLabel statusLbl;

    public SetupPanel(){

        super();
        //add all of the JRadioButtons
        //add them to a button group

    }

private class LevelHandler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == begButton){
            speed = 100000;
        }


        if(e.getSource() == intButton){
            speed = 400000;
        }


        if(e.getSource() == advButton){
                speed = 700000;
            }

            setFocusable(false);   //Doesn't seem to make a difference

        }

    }

}
那么,我最困惑的是我的蛇形板:

public class SnakePanel extends JPanel implements Runnable{
    public SnakePanel() {

        setFocusable(true); //Focus on this panel or snake won't move

        //I have also tried these lines to keep focus on this 
        //panel but it doesn't work either
        //this.addFocusListener(new FocusAdapter() {
        //   public void focusLost(FocusEvent ev) {
        //   requestFocus();
        //   }
        //  });

        //set what it will look like, size, etc...

        this.addKeyListener(new Key());
        startMoving();
    }


    private void startMoving(){
        running = true;
        thread = new Thread(this, "snake movement");
        thread.start();
    }

    @Override
    public void run() {
        while(running){
            move();       //things program does each time snake moves
            repaint();
        }
    }

    //KeyListeners

}
我很确定这是一个焦点问题

对。现在,重点是单选按钮,而不是添加KeyListener的组件

并使用箭头键控制蛇

更好的解决方案是使用
键绑定
。即使组件有焦点,键绑定仍然可以工作

查看更多信息和工作示例

我很确定这是一个焦点问题

对。现在,重点是单选按钮,而不是添加KeyListener的组件

并使用箭头键控制蛇

更好的解决方案是使用
键绑定
。即使组件有焦点,键绑定仍然可以工作

查看更多信息和工作示例。

使用键绑定

this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "right");
    this.getActionMap().put("right", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            //move certain direction
        }
    });
并按不同的键重复使用键绑定

this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "right");
    this.getActionMap().put("right", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            //move certain direction
        }
    });
然后按不同的键重复