在交换jpanel之后,不会触发KeyListener

时间:2022-05-31 23:57:53

Im making a game and Im having it so that when the user presses "I" in the game, the game panel is set to invisible while it adds the Inventory panel to the JFrame. Then when the user exits the Inventory it will remove the Inventory JPanel and then set back the game JPanel to visible.

我正在制作一个游戏,当用户在游戏中按下“I”时,游戏面板被设置为不可见,同时它将库存面板添加到JFrame中。然后当用户退出库存时,它将删除库存JPanel,然后将game JPanel设置为可见。

Now this all sounds good, but whenever it removes the Inventory JPanel and goes back to the game JPanel, the KeyListener stops working. I even set back the setFocusable(true) property back on the game JPanel after I remove the Inventory JPanel but it still doesn't make the KeyListener work.

现在这一切听起来都不错,但是每当它删除了Inventory JPanel并返回到game JPanel时,KeyListener就停止工作了。我甚至在删除了Inventory JPanel之后,将setFocusable(true)属性设置回游戏JPanel,但它仍然不能让KeyListener工作。

Here is my code for the game Jpanel:

这是我的游戏Jpanel代码:

package javavideogame;

public class Game extends JPanel implements ActionListener, Runnable
{

    public Game(MainCharacter character)
    {
        TAdapter a = new TAdapter();
        addKeyListener(a);
        setFocusable(true);
        setDoubleBuffered(true);
        setFocusTraversalKeysEnabled(false);
    }

    public void getInventoryScreen()
    {
        Main.inv = new Inventory();
        Main.inv.sayHello();
        Main.mainGame.getContentPane().add(Main.inv);
        Main.game.setVisible(false);
        Main.mainGame.validate();
    }

    public void closeInventory()
    {
        Main.inv.setFocusable(false);
        Main.mainGame.remove(Main.inv);
        Main.game.setVisible(true);
        Main.game.setFocusable(true);
    }

    public class TAdapter extends KeyAdapter
    {
        public void keyPressed(KeyEvent e)
        {
            character.keyPressed(e);
        }

        public void keyReleased(KeyEvent e)
        {
            character.keyReleased(e);
        }
    }

}

And here is the Inventory code:

这是库存代码:

package javavideogame;

public class Inventory extends JPanel implements KeyListener
{
    public Inventory()
    {
        setBackground(Color.RED);
        addKeyListener(this);
        setFocusable(true);
    }

    public void keyPressed(KeyEvent e)
    {
        int key = e.getKeyCode();
        if(key == KeyEvent.VK_I)
        {
            Main.game.closeInventory();
        }
    }

    public void keyReleased(KeyEvent e)
    {

    }

    public void keyTyped(KeyEvent e)
    {

    }
}

And yes im having a hard time getting the code thing on here to work right :)

是的,我很难让代码在这里正常工作:)

But is there something i can easily put into the code so that the KeyListener will actually work right once it goes back to the game JPanel?

但是有什么东西我可以很容易地放到代码中,以便键监听器在回到游戏JPanel之后能够正常工作吗?

2 个解决方案

#1


5  

I even set back the setFocusable(true) property back on the game JPanel after I remove the Inventory JPanel

在删除了Inventory JPanel之后,我甚至将setfocable (true)属性设置回游戏JPanel

Key events only go the the component that has focus. You need to invoke:

关键事件只发生在有焦点的组件上。你需要调用:

panel.requestFocusInWindow();

after swapping the panels to make sure the panel has focus again.

在交换面板之后,确保面板再次有焦点。

However, a better approach is to use Key Bindings itead of a KeyListener.

然而,更好的方法是使用密匙监听器的密匙绑定。

#2


0  

You could skip working with adding and removing panels to the content pane and just set the visibility. Then you should also skip setting the focusable property (KeyEvents won't be passed to an invisible component anyway) and your key listeners should be preserved and come into effect again when the component becomes visible.

您可以跳过向内容窗格添加和删除面板的工作,而只需设置可视性。然后,您还应该跳过设置可调焦属性(无论如何不会将KeyEvents传递给不可见的组件),并且应该保留您的关键监听器,并在组件变得可见时再次生效。

#1


5  

I even set back the setFocusable(true) property back on the game JPanel after I remove the Inventory JPanel

在删除了Inventory JPanel之后,我甚至将setfocable (true)属性设置回游戏JPanel

Key events only go the the component that has focus. You need to invoke:

关键事件只发生在有焦点的组件上。你需要调用:

panel.requestFocusInWindow();

after swapping the panels to make sure the panel has focus again.

在交换面板之后,确保面板再次有焦点。

However, a better approach is to use Key Bindings itead of a KeyListener.

然而,更好的方法是使用密匙监听器的密匙绑定。

#2


0  

You could skip working with adding and removing panels to the content pane and just set the visibility. Then you should also skip setting the focusable property (KeyEvents won't be passed to an invisible component anyway) and your key listeners should be preserved and come into effect again when the component becomes visible.

您可以跳过向内容窗格添加和删除面板的工作,而只需设置可视性。然后,您还应该跳过设置可调焦属性(无论如何不会将KeyEvents传递给不可见的组件),并且应该保留您的关键监听器,并在组件变得可见时再次生效。