java 如何在Java中激活窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4005491/
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-10-30 04:19:59  来源:igfitidea点击:

How to activate window in Java?

javaswing

提问by AlexR

I would like to activate my Swing application programatically. I mean that I would like to write code that cause the JFrameto be visible and focused (the window header should be highlighted). I tried to use requestFocus(). It works only if application has at least 2 windows A and B: A is hidden, B is visible. Now if I call A.requestFocus()it becomes active as I want. It does not happen if application has only one window or if both windows are invisible.

我想以编程方式激活我的 Swing 应用程序。我的意思是我想编写使JFrame可见并聚焦的代码(应突出显示窗口标题)。我尝试使用requestFocus(). 仅当应用程序至少有 2 个窗口 A 和 B 时才有效:A 是隐藏的,B 是可见的。现在,如果我调用A.requestFocus()它,它会根据我的需要变得活跃。如果应用程序只有一个窗口或两个窗口都不可见,则不会发生这种情况。

I found 2 workarounds.

我找到了 2 个解决方法。

  1. use fake transparent undecorated frame that is always on top. This fake window will play role of window B. I did not try to implement it but it seems that it should work.
  2. call A.setAlwaysOnTop(true). This brings window A on top of other windows. But it is not in focus yet. Use java.awt.Robot(mouseMove, mousePress, mouseRelease) to make a click on the header of window A. Now call A.setAlwaysOnTop(false)and return mouse pointer back to its previous position. I implemented the code and it works but it looks like an ugly workaround.
  1. 使用始终在顶部的假透明未装饰框架。这个假窗口将扮演窗口 B 的角色。我没有尝试实现它,但它似乎应该可以工作。
  2. 打电话A.setAlwaysOnTop(true)。这会将窗口 A 置于其他窗口之上。但目前还不是重点。使用java.awt.Robot(mouseMove, mousePress, mouseRelease) 单击窗口 A 的标题。现在调用A.setAlwaysOnTop(false)并将鼠标指针返回到其先前的位置。我实现了代码并且它有效,但它看起来像一个丑陋的解决方法。

Is there a "right" solution?

有“正确”的解决方案吗?

采纳答案by 01es

I believed that thispost should help you.

我相信这篇文章应该对你有所帮助。

回答by Brad Mace

frame.setState(Frame.NORMAL); // restores minimized windows
frame.toFront(); // brings to front without needing to setAlwaysOnTop
frame.requestFocus();

for everything you could want to know in excruciating detail, see this page: http://www.developer.com/java/other/article.php/3502181/Window-Focus-and-State-in-Java.htm

对于您可能想知道的所有细节,请参阅此页面:http: //www.developer.com/java/other/article.php/3502181/Window-Focus-and-State-in-Java.htm

回答by user3654656

I was in the same boat - none of the above worked.

我在同一条船上 - 以上都没有奏效。

"MY" solution was follows:

“我的”解决方案如下:

thisFrame.getWindowListeners()[0].windowActivated(
     new WindowEvent(
              thisFrame,
              WindowEvent.WINDOW_ACTIVATED
     )
);
schedulesTable.requestFocus();

thisFrame = the window to get activated

schedulesTable = my component in the window I wanted to get focus for

回答by Dmitriy Potapov

I've found out this solvation of the problem:

我发现了这个问题的解决方案:

//frame - JFrame
frame.setExtendedState(JFrame.ICONIFIED);
frame.setExtendedState(JFrame.NORMAL);
frame.toFront();
frame.requestFocus();

On my configuration (win 7, java 12) - it works alright and stably

在我的配置上(win 7,java 12) - 它运行良好且稳定

回答by Michael Goldshteyn

This should do it:

这应该这样做:

frame.setSelected(true);

and you probably want it inside a try/catch block...

你可能想把它放在一个 try/catch 块中......

If that doesn't work on the OS you're using, there are two more possibilities:

如果这在您使用的操作系统上不起作用,还有两种可能性:

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

and

frame.setVisible(true);
frame.setVisible(true); // Yes you need this second one