显示最小化的JFrame窗口

显示最小化的JFrame窗口

本文介绍了Java - 显示最小化的JFrame窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个JFrame窗口被最小化,有什么办法让它重新聚焦吗?

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

  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 返回,您可以将其状态设置为正常

  JFrame frame = new JFrame(...); 
//显示框架
frame.setVisible(true);

//睡眠5秒钟,然后最小化
Thread.sleep(5000);
frame.setState(java.awt.Frame.ICONIFIED);

//睡眠5秒,然后恢复
Thread.sleep(5000);
frame.setState(java.awt.Frame.NORMAL);

来自,每当状态发生变化时触发一个接口来处理这些触发器。在这种情况下,您可以使用:

  public class YourClass implements WindowListener {
...
public void windowDeiconified(WindowEvent e){
//当窗口被恢复时做一些事情
}
}

如果您想要检查另一个程序的状态更改,则不存在纯Java解决方案,只需要获取窗口的 ID

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));
            }
解决方案

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);

Example from here.

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
  }
}

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.

这篇关于Java - 显示最小化的JFrame窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:11