交换 JPanel 后未触发 KeyListener
我正在制作一个游戏,并且我拥有它,以便当用户在游戏中按“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按键事件仅进入具有焦点的组件。您需要
在交换面板后调用:以确保面板再次获得焦点。
但是,更好的方法是使用按键绑定按键监听器。
Key events only go the the component that has focus. You need to invoke:
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.
您可以跳过向内容窗格添加和删除面板的操作,而只需设置可见性。然后,您还应该跳过设置可聚焦属性(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.