是否可以将 JFrame 放在前面但不聚焦?

2022-01-24 00:00:00 java swing jframe

我正在编写一个定期弹出 JFrame 的 Java 应用程序(Swing GUI).

I'm writing a Java app (Swing GUI) that periodically pops up a JFrame.

是否有可能以某种方式将窗口置于前面(foo.setAlwaysOnTop(true) 会更好)但没有使其成为焦点?

Is it possible somehow to bring the window to front (foo.setAlwaysOnTop(true) would be even better) but without having it focus?

有些人在打字时会不时将视线从屏幕上移开看他们的键盘,我敢肯定,如果这个窗口总是能捕捉到键盘焦点,人们会非常恼火,因为这会导致他们迷失方向每次弹出时都会被击键很多次.

Some people move their eyes away from the screen from time to time to look at their keyboard while typing, and I'm sure that if this window would always capture the keyboard focus people would get really annoyed as it's causing them to lose quite a few keystrokes every time it pops up unnoticed.

在其他情况下,即使用户实际上能够在不一直看键盘的情况下打字,弹出一个窗口并获得焦点也可能会导致弹出窗口本身的不需要的操作(一些 Tab+Enter 组合,例如,用户不小心选择了一个她本来不会选择的选项).

In other cases, even when the user is actually capable of typing without looking at the keyboard all the time, having a window pop up and get focus could cause unwanted actions from the pop-up window itself (some Tab+Enter combination for example, where the user accidentally selects an option she really wouldn't had selected otherwise).

提前致谢!

更新

正如 Jonas 建议的那样,如果在渲染窗口后调用 foo.setFocusableWindowState(false); 似乎可以工作(仅在 Gnome 上测试过).

As Jonas suggests, foo.setFocusableWindowState(false); seems to work if called after the window has been rendered (tested on Gnome only).

这不起作用:

foo.setFocusableWindowState(false);
foo.setVisible(true);
foo.setFocusableWindowState(true);

但是,这确实:

foo.setFocusableWindowState(false);
foo.setVisible(true);
Thread.sleep(1000);
foo.setFocusableWindowState(true);

我必须看看是否有我可以捕捉/收听的事件允许我在适当的时候执行 foo.setFocusableWindowStatue(true);.

I'll have to see if there's an event I can catch/listen to that allows me to do foo.setFocusableWindowStatue(true); when appropriate.

我认为我的问题已经解决了.

I consider my problem solved.

推荐答案

这可能有效:

foo.setFocusableWindowState(false);

相关文章