交换 JPanel 后未触发 KeyListener

发布于 2024-10-18 10:10:11 字数 2010 浏览 3 评论 0原文

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

现在这一切听起来不错,但是每当它删除 Inventory JPanel 并返回到游戏 JPanel 时,KeyListener 就会停止工作。在删除 Inventory JPanel 后,我什至将 setFocusable(true) 属性设置回游戏 JPanel 上,但它仍然无法使 KeyListener 工作。

这是我的游戏 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);
        }
    }

}

这是库存代码:

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)
    {

    }
}

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

但是有什么东西我可以轻松地放入代码中,以便 KeyListener 能够返回游戏 JPanel 后是否真的可以正常工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2024-10-25 10:10:11

在删除 Inventory JPanel 后,我什至将 setFocusable(true) 属性设置回游戏 JPanel

按键事件仅进入具有焦点的组件。您需要

panel.requestFocusInWindow();

在交换面板后调用:以确保面板再次获得焦点。

但是,更好的方法是使用按键绑定按键监听器。

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.

悍妇囚夫 2024-10-25 10:10:11

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文