开发者

KeyListener not being triggered after I swap JPanels

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.

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.

Here is my code for the game Jpanel:

package javavideogame;

public class Game extends JPanel implements ActionListener, Runnable
{

    public Game(MainCharacter character)
    {
        TAdapter a = new TAdapter();开发者_Python百科
        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?


I even set back the setFocusable(true) property back on the game JPanel after I remove the Inventory 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜