如何使用Java捕获JWindow中的键盘输入?

编程入门 行业动态 更新时间:2024-10-22 18:37:18
本文介绍了如何使用Java捕获JWindow中的键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我用F1到12或0到9或A到Z按键盘(所有按钮)时.我看不到它捕获了我的键盘输入.我该如何解决?

When i press keyboard with F1 to 12 or 0 to 9 or A to Z (all buttons). I do not see its capturing my keyboard inputs. How do i fix this?

public class Boot extends JWindow implements KeyListener { public Boot() { ..... this.addKeyListener(this); .... } public void keyTyped(KeyEvent ke) { System.out.println( ke.getKeyChar()); } public void keyPressed(KeyEvent ke) { System.out.println( ke.getKeyChar()); /* KEY EVENTS */ // KeyEvent.KEY_TYPED // KeyEvent.KEY_PRESSED // int id = id.getId(); } public void keyReleased(KeyEvent ke) { System.out.println( ke.getKeyChar()); } }

推荐答案

KeyEvent仅传递给可聚焦的组件.

KeyEvents are only passed to components that are focusable.

阅读JWindow()构造函数的API.它指出:

Read the API for the JWindow() constructor. It states:

创建一个没有指定所有者的窗口.此窗口将无法聚焦.

Creates a window with no specified owner. This window will not be focusable.

阅读JWindow(Frame)构造函数的API.它指出:

Read the API for the JWindow(Frame) constructor. It states:

使用指定的所有者框架创建一个窗口.如果owner为null,则将使用共享所有者,并且该窗口将无法聚焦.此外,除非窗口的所有者显示在屏幕上,否则该窗口将无法聚焦.

Creates a window with the specified owner frame. If owner is null, the shared owner will be used and this window will not be focusable. Also, this window will not be focusable unless its owner is showing on the screen.

因此,基本上,在使用JWindow时,您还需要创建一个可见的JFrame.

So basically you also need to create a visible JFrame when using a JWindow.

JFrame frame = new JFrame(); frame.setVisible( true ); JWindow window = new JWindow(frame);

我在论坛上看到的一种骇客是要使用的:

A hack I've seen on the forums is to use:

JWindow window = new JWindow(new JFrame("is Showing") { public boolean isShowing() { return true; } });

或者更好的方法是使用未修饰的JFrame,而您不必为此担心.

Or a better approach is to use an undecorated JFrame and you don't have to worry about this.

更多推荐

如何使用Java捕获JWindow中的键盘输入?

本文发布于:2023-11-07 05:27:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   键盘输入   Java   JWindow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!