Java - 显示最小化的JFrame窗口

Str*_*ies 6 java swing focus 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));
            }
Run Code Online (Sandbox Code Playgroud)

Jon*_*and 12

如果你想让它恢复原状iconified,你可以将其状态设置为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);
Run Code Online (Sandbox Code Playgroud)

这里的例子.

也有WindowEvent小号每当状态改变时触发和WindowListener处理这些triggers.In这种情况下接口,您可以使用:

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

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


Jon*_*nas 5

您可以将状态设置为正常:

frame.setState(NORMAL);
Run Code Online (Sandbox Code Playgroud)

完整示例:

public class FrameTest extends JFrame {

    public FrameTest() {
        final JFrame miniFrame = new JFrame();
        final JButton miniButton = new JButton(
          new AbstractAction("Minimize me") {
            public void actionPerformed(ActionEvent e) {
                miniFrame.setState(ICONIFIED);
            }
        }); 

        miniFrame.add(miniButton);
        miniFrame.pack();
        miniFrame.setVisible(true);

        add(new JButton(new AbstractAction("Open") {
            public void actionPerformed(ActionEvent e) {
                miniFrame.setState(NORMAL);
                miniFrame.toFront();
                miniButton.requestFocusInWindow();
            }
        }));

        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new FrameTest();
    }

}
Run Code Online (Sandbox Code Playgroud)