如何使用Java捕获JWindow中的键盘输入? [英] How to capture keyboard inputs in my JWindow using Java?

查看:354
本文介绍了如何使用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中的键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆