如何从 Java 最小化 JFrame 窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3965336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:42:02  来源:igfitidea点击:

How to minimize a JFrame window from Java?

javaswingjframeminimize

提问by Frank

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

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

采纳答案by Brad Mace

minimize with frame.setState(Frame.ICONIFIED)

最小化 frame.setState(Frame.ICONIFIED)

restore with frame.setState(Frame.NORMAL)

恢复与 frame.setState(Frame.NORMAL)

回答by Brad Mace

You can do this in two ways:

您可以通过两种方式执行此操作:

JFrame frame = new JFrame("Test");

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

回答by Rusiru Adithya Samarasinghe

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

回答by Buddhi Kavindra

You can use following code:

您可以使用以下代码:

this.setState(YourJFrame.ICONIFIED);

And you can use this code to maximize it:

您可以使用此代码来最大化它:

this.setExtendedState(MAXIMIZED_BOTH);

回答by Aaron Esau

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 JFramedocumentationfor more information.

有关更多信息,请参阅JFrame文档

回答by OscarMike

Another approach

另一种方法

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