之后我换JPanels的KeyListener未触发 [英] KeyListener not being triggered after I swap JPanels

查看:111
本文介绍了之后我换JPanels的KeyListener未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林做一个游戏,有它我好,当用户presses我的游戏,游戏面板被设置为不可见,而其添加清单面板到JFrame。然后,当用户退出库存它会删除清单的JPanel,然后重新设置游戏的JPanel可见。

现在这一切听起来不错,但每当它消除了库存JPanel并追溯到比赛的JPanel,该的KeyListener停止工作。我甚至倒退setFocusable(真)财产回到了比赛的JPanel后,我取出库存的JPanel,但它仍然不会使的KeyListener工作。

下面是我的code为游戏的JPanel:

 包javavideogame;公共类游戏继承JPanel实现的ActionListener,Runnable接口
{    公共游戏(MainCharacter字符)
    {
        TAdapter一个=新TAdapter();
        addKeyListener(一);
        setFocusable(真);
        setDoubleBuffered(真);
        setFocusTraversalKeysEnabled(假);
    }    公共无效getInventoryScreen()
    {
        Main.inv =新的库存();
        Main.inv.sayHello();
        Main.mainGame.getContentPane()加(Main.inv)。
        Main.game.setVisible(假);
        Main.mainGame.validate();
    }    公共无效closeInventory()
    {
        Main.inv.setFocusable(假);
        Main.mainGame.remove(Main.inv);
        Main.game.setVisible(真);
        Main.game.setFocusable(真);
    }    公共类TAdapter扩展KeyAdapter
    {
        公共无效键pressed(KeyEvent e而)
        {
            character.key pressed(E);
        }        公共无效调用keyReleased(KeyEvent e而)
        {
            character.keyReleased(E);
        }
    }}

这里是清单code:

 包javavideogame;公共类库存继承JPanel实现的KeyListener
{
    公共库存()
    {
        的setBackground(Color.RED);
        addKeyListener(本);
        setFocusable(真);
    }    公共无效键pressed(KeyEvent e而)
    {
        INT键= e.getKey code();
        如果(键== KeyEvent.VK_I)
        {
            Main.game.closeInventory();
        }
    }    公共无效调用keyReleased(KeyEvent e而)
    {    }    公共无效的keyTyped(KeyEvent e而)
    {    }
}

和是有一个很难得到code的事情就在这里工作的权利IM:)

不过是有什么我可以轻松地放进code这样的KeyListener将实际的工作权利,一旦回到本场​​比赛的JPanel?


解决方案

  

我甚至倒退setFocusable(真)财产回到了比赛的JPanel后,我取出库存的JPanel


关键事件只能走具有焦点的组件。你需要调用:

  panel.requestFocusInWindow();

交换面板以确保面板具有重新聚焦后

然而,更好的方法是使用的KeyListener的键绑定 itead。

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();
        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.

这篇关于之后我换JPanels的KeyListener未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆