java - KeyListener问题

标签 java graphics jpanel keylistener

在我的应用程序中,我使用了一个 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 是否有键盘焦点?

我建议您使用 InputMapWHEN_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/

相关文章:

r - 使用 R 中的 levelplot 将不同的多边形添加到栅格堆栈图中

java - 如何在 JPanel 上使用 9 补丁图像作为背景?

java - 切换 Jpanels 1 Jframe

java - Jung Graph 未显示在 Jpanel 上

java - 带 EditText 和 InputType 电子邮件的 AlertDialog - Android

java - 类似 Photoshop 的脚本框架

java - Gridbag布局问题

java - Apache James 邮件服务器 - 将附件保存在文件系统上

java - 有没有办法根据线程 "attached"的目标来控制线程?

c# - 在 C# 中将字符串转换为画笔/画笔颜色名称