How to minimize a JFrame window from Java?

为君一笑 提交于 2019-11-26 18:23:18

问题


In my Java app, I have a JFrame window, how can I minimize it from my Java program ?


回答1:


minimize with frame.setState(Frame.ICONIFIED)

restore with frame.setState(Frame.NORMAL)




回答2:


Minimize:

frame.setState(Frame.ICONIFIED);

Another way to minimize:

frame.setExtendedState(JFrame.ICONIFIED);

Normal size:

frame.setState(Frame.NORMAL);

Another way to normal size:

frame.setExtendedState(JFrame.NORMAL);

Maximize:

frame.setState(Frame.MAXIMIZED_BOTH);

Another way to maximize:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

Full Screen maximize:

GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
try { device.setFullScreenWindow((Window) frame); } finally { device.setFullScreenWindow(null); }

Refer to the JFrame documentation for more information.




回答3:


you can do this in two ways

JFrame frame = new JFrame("test");
 frame.setExtendedState(JFrame.ICONIFIED);  // one way


    frame.setState(JFrame.ICONIFIED); // another way



回答4:


Another approach

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED));



回答5:


If you are trying to code for a event of a component then try code below. And make sure the class which this code is included is extended by Frame class

private void closeMouseClicked(java.awt.event.MouseEvent evt){                        
    this.setState(1);
}

Or create an instance of a Frame class and call setState(1);




回答6:


You can use following code:

this.setState(YourJFrame.ICONIFIED);

And you can use this code to maximize it:

this.setExtendedState(MAXIMIZED_BOTH);


来源:https://stackoverflow.com/questions/3965336/how-to-minimize-a-jframe-window-from-java

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