我想以编程方式激活我的Swing应用程序.我的意思是我想编写导致JFrame可见和聚焦的代码(窗口标题应突出显示).我试着用requestFocus().仅当应用程序至少有2个窗口A和B时才有效:A隐藏,B可见.现在如果我打电话A.requestFocus()它就像我想要的那样变得活跃.如果应用程序只有一个窗口或两个窗口都不可见,则不会发生这种情况.
我找到了2个解决方法.
A.setAlwaysOnTop(true).这使窗口A位于其他窗口之上.但它还没有成为焦点.使用java.awt.Robot(mouseMove,mousePress,mouseRelease)在窗口A的标题上单击.现在调用A.setAlwaysOnTop(false)并将鼠标指针返回到其先前的位置.我实现了代码并且它可以工作,但它看起来像一个丑陋的解决方法.有没有"正确"的解决方案?
frame.setState(Frame.NORMAL); // restores minimized windows
frame.toFront(); // brings to front without needing to setAlwaysOnTop
frame.requestFocus();
Run Code Online (Sandbox Code Playgroud)
对于您可能想要了解的一切细节,请参阅此页:http://www.developer.com/java/other/article.php/3502181/Window-Focus-and-State-in-Java.htm
这应该可以做到:
frame.setSelected(true);
Run Code Online (Sandbox Code Playgroud)
你可能希望它位于 try/catch 块中......
如果这在您使用的操作系统上不起作用,还有两种可能性:
frame.setAlwaysOnTop(true);
frame.setAlwaysOnTop(false);
Run Code Online (Sandbox Code Playgroud)
和
frame.setVisible(true);
frame.setVisible(true); // Yes you need this second one
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13415 次 |
| 最近记录: |