Java - Show a minimized JFrame window

拈花ヽ惹草 提交于 2019-12-01 05:16:08

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.

You can set the state to normal:

frame.setState(NORMAL);

Full example:

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!