Java 如何聚焦 JFrame?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/641172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:17:22  来源:igfitidea点击:

How to focus a JFrame?

javauser-interfaceswingjframe

提问by John Murano

I am writing a small game, with one JFrame that holds the main game, and another JFrame that displays the score. the problem is, when I am done constructing them, the score JFrame always ends up focused! I have tried calling scoreDisplay.toFront(), scoreDisplay.requestFocus(), and even:

我正在编写一个小游戏,其中一个 JFrame 保存主游戏,另一个 JFrame 显示分数。问题是,当我完成构建它们时,分数 JFrame 总是最终集中!我曾尝试调用 scoreDisplay.toFront()、scoreDisplay.requestFocus(),甚至:

display.setState(JFrame.ICONIZED);
display.setState(JFrame.NORMAL);

Is there any way to make this work? Thanks in advance, john murano

有什么办法可以使这项工作?提前致谢,约翰穆拉诺

采纳答案by OscarRyz

Have you consider setting the score in the same frame as the game frame?

有没有考虑把比分设置在与游戏框架相同的框架内?

Other possible ( quick and dirty ) option is to create them in reverse order, or at least ( if score depends on game ) display them in reverse order.

其他可能的(快速和肮脏的)选项是以相反的顺序创建它们,或者至少(如果分数取决于游戏)以相反的顺序显示它们。

score.setVisible( true );
game.setVisible( true );

My guess is that currently they are:

我的猜测是,目前它们是:

game.setVisible( true );
score.setVisible( true );

回答by Pooria

The way that I would do is:

我会做的方式是:

 frame.toFront();
 frame.setState(Frame.NORMAL); 

and If you also want have more control on it you should use requestFocuse.

如果您还想对其进行更多控制,则应使用 requestFocuse。

BTW, here is an example : http://coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2006-06/msg00152.html

顺便说一句,这里是一个例子:http: //coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2006-06/msg00152.html

回答by ordnungswidrig

Toggle alwaysOnTop

切换 alwaysOnTop

See here:

看这里:

http://forums.sun.com/thread.jspa?threadID=5124278

http://forums.sun.com/thread.jspa?threadID=5124278

Read about toFront in the API http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html#toFront

Some platforms may not permit this VM to place its Windows above windows of native applications, or Windows of other VMs.

On Windows OS for example toFront causes the icon on the Task Bar to flicker, but the window stays in the back.

The only think that will force the window to front is setAlwaysOnTop.

阅读 API http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html#toFront 中的toFront

某些平台可能不允许此 VM 将其 Windows 置于本机应用程序的窗口或其他 VM 的 Windows 之上。

例如,在 Windows 操作系统上, toFront 会导致任务栏上的图标闪烁,但窗口停留在后面。

唯一认为会强制窗口位于前面的是 setAlwaysOnTop。

frame.setAlwaysOnTop(true); 
frame.setAlwaysOnTop(false);

回答by Kiran

frame.setExtendedState( JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

Try the above..

试试上面的..

回答by Erick Robertson

Call the requestFocus()method.

调用requestFocus()方法。

This is not guaranteed to work, because there are many reasons why an operating system would not allow a frame to have focus. There could be another frame with higher priority in a different application. There are also some linux desktops which (if I recall correctly) do not allow frames to request focus.

这不能保证工作,因为操作系统不允许框架具有焦点的原因有很多。在不同的应用程序中可能有另一个具有更高优先级的帧。还有一些 linux 桌面(如果我没记错的话)不允许框架请求焦点。

To give you a better chance of success, I also recommend calling the toFront()method before requesting focus.

为了给你更好的成功机会,我还建议toFront()在请求焦点之前调用该方法。

frame.setVisible(true);
frame.toFront();
frame.requestFocus();

Please keep in mind, none of this is guaranteed because frame handling, especially with focus and layering, is very operating system-dependant. So set the frame to visible, move it to the front, and request the focus. Once you give up the EDT, the operating system will likely give the frame the focus. At the very least, the window should be on top.

请记住,这一切都不能保证,因为帧处理,尤其是焦点和分层,非常依赖于操作系统。因此,将框架设置为可见,将其移到前面,然后请求焦点。一旦您放弃 EDT,操作系统可能会将重点放在框架上。至少,窗户应该在上面。

回答by Paki

Just add the following line of code above the JOptionPaneMessageDialog code ... this.setAlwaysOnTop(false);

只需在 JOptionPaneMessageDialog 代码上方添加以下代码行... this.setAlwaysOnTop(false);