简体   繁体   English

如何从 Java 最小化 JFrame 窗口?

[英]How to minimize a JFrame window from Java?

在我的 Java 应用程序中,我有一个 JFrame 窗口,如何将它从我的 Java 程序中最小化?

minimize with frame.setState(Frame.ICONIFIED)使用frame.setState(Frame.ICONIFIED)最小化

restore with frame.setState(Frame.NORMAL)使用frame.setState(Frame.NORMAL)恢复

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.有关更多信息,请参阅JFrame文档

You can do this in two ways:您可以通过两种方式执行此操作:

JFrame frame = new JFrame("Test");

frame.setExtendedState(JFrame.ICONIFIED); // One way
frame.setState(JFrame.ICONIFIED); // Another way

另一种方法

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

You can use following code:您可以使用以下代码:

this.setState(YourJFrame.ICONIFIED);

And you can use this code to maximize it:您可以使用此代码来最大化它:

this.setExtendedState(MAXIMIZED_BOTH);

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并确保包含此代码的类由 Frame 类扩展

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

Or create an instance of a Frame class and call setState(1);或者创建一个Frame类的实例并调用setState(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM