简体   繁体   English

如何失去对JFrame的关注?

[英]How do I lose the focus on a JFrame?

I'm making some kind of on-screen keyboard in java. 我在java中制作某种屏幕键盘。 So I need the JFrame to lose focus (and thus, transfer focus to the next window) to send the key strokes. 所以我需要JFrame失去焦点(因此,将焦点转移到下一个窗口)来发送击键。 Is there a way to make the JFrame window lose focus? 有没有办法让JFrame窗口失去焦点?

For example, on Windows, if you open the native on-screen keyboard, you can press the buttons on there while the on-screen window itself is not in focus but instead, a different window gets the input. 例如,在Windows上,如果打开本机屏幕键盘,则可以在屏幕窗口本身未对焦的同时按下那里的按钮,而是使用不同的窗口获取输入。 在此输入图像描述

For clarity, I'm trying to replicate what the native on-screen keyboard does. 为清楚起见,我试图复制原生屏幕键盘的功能。 So the one I'm making will be used on any and all windows that I have open, not just Java. 所以我正在制作的那个将用于我打开的任何和所有窗口,而不仅仅是Java。 I'm using the Robot class to simulate the keystrokes. 我正在使用Robot类来模拟击键。

To sum it off, I need my JFrame to: 总结一下,我需要我的JFrame:

  • Stay on top of other windows (Done. By using setAlwaysOnTop(true); ) 保持在其他窗口的顶部(完成。通过使用setAlwaysOnTop(true);
  • Not have focus (So the key strokes can be send to whatever the top-level window is at the moment instead) 没有焦点(因此,关键笔划可以发送到目前顶级窗口的任何位置)

I can't believe how stupid I am, the answer was right there. 我无法相信我有多愚蠢,答案就在那里。 I don't know how I missed it but setFocusableWindowState(false); 我不知道我是怎么错过它但是setFocusableWindowState(false); is what I was looking for. 是我在寻找的。 Using that flag on the JFrame, it doesn't get focused. 在JFrame上使用该标志,它不会得到集中。

I'm guessing that your second window is another JFrame, perhaps? 我猜你的第二个窗口是另一个JFrame,也许? If so, put your secondary window in a JDialog that is linked to the primary main JFrame (that is, you pass the JFrame into the JDialog's constructor). 如果是这样,请将辅助窗口放在链接到主要主JFrame的JDialog中(即,将JFrame传递给JDialog的构造函数)。 This will give you exactly what you want. 这将为您提供您想要的。

For more specific help, consider creating a very simple and small example program that demonstrates your problem. 有关更具体的帮助,请考虑创建一个非常简单的小示例程序来演示您的问题。 It could have just one JButton on the dialog window for instance, an sscce . 例如,它可以在对话框窗口上只有一个JButton,一个sscce


Edit 编辑
After reading your updated question, I fear that your problem can not be solved with core Java and will require some native coding, perhaps through JNI or JNA. 在阅读了更新后的问题后,我担心您的问题无法通过核心Java解决,并且可能需要一些本机编码,可能通过JNI或JNA。 Please understand that in order for Java to be usable on multiple platforms, it was built to be as OS agnostic as possible and thus is required to be isolated from low-level OS calls, calls that would be required for your desired program to work. 请理解,为了使Java可以在多个平台上使用,它被构建为尽可能与操作系统无关,因此需要与低级操作系统调用隔离,这是您所需程序工作所需的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM