在我的应用程序中,我使用了一个 jpanel,我想在其中添加一个关键监听器。我做到了。但它不起作用。
是不是因为我在用swingworker每秒更新面板的内容。
这是我更新面板的代码
RenderedImage image = ImageIO.read(new ByteArrayInputStream((byte[]) get()));
Graphics graphics = remote.rdpanel.getGraphics();
if (graphics != null) {
Image readyImage = new ImageIcon(UtilityFunctions.convertRenderedImage(image)).getImage();
graphics.drawImage(readyImage, 0, 0, remote.rdpanel.getWidth(), remote.rdpanel.getHeight(), null);
}
最佳答案
JPanel
是否有键盘焦点?
我建议您使用 InputMap
和 WHEN_IN_FOCUSED_WINDOW
或类似的东西。摘自How to Use Key Bindings :
The WHEN_IN_FOCUSED_WINDOW input maps of all the enabled components in the focused window are searched. Because the order of searching the components is unpredictable
这对我来说非常有效。查看我的其他帖子以获取更多信息和实际代码示例:
或本教程:Swing: Understanding Input/Action Maps
相关问题:
关于java - KeyListener问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2832062/