Java - 显示最小化的 JFrame 窗口

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

如果一个 JFrame 窗口被最小化了,有什么方法可以让它回到焦点?

If a JFrame window is minimized, is there any way to bring it back to focus?

我试图让它点击某个点,然后恢复它.

I am trying to get it to click a certain point, then restore it.

            while (isRunning) {
                start = System.currentTimeMillis();
                frame.setState(Frame.ICONIFIED);
                robot.mouseMove(clickX, clickY);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                frame.setState(Frame.NORMAL);
                Thread.sleep(clickMs - (System.currentTimeMillis() - start));
            }

推荐答案

如果你想把它从iconified中恢复过来,你可以把它的状态设置为normal:

If you want to bring it back from being iconified, you can just set its state to normal:

JFrame frame = new JFrame(...);
// Show the frame
frame.setVisible(true);

// Sleep for 5 seconds, then minimize
Thread.sleep(5000);
frame.setState(java.awt.Frame.ICONIFIED);

// Sleep for 5 seconds, then restore
Thread.sleep(5000);
frame.setState(java.awt.Frame.NORMAL);

示例来自这里.

还有WindowEvents 每当状态改变时触发,而 WindowListener 接口处理这些触发器.在这种情况下,您可以使用:

There are also WindowEvents that are triggered whenever the state is changed and a WindowListener interface that handles these triggers.In this case, you might use:

public class YourClass implements WindowListener {
  ...
  public void windowDeiconified(WindowEvent e) {
    // Do something when the window is restored
  }
}

如果你想检查另一个程序的状态变化,没有纯Java"的解决方案,只需要获取窗口的ID.

If you are wanting to check another program's state change, there isn't a "pure Java" solution, but just requires getting the window's ID.

相关文章