简体   繁体   English

按下窗口关闭按钮时如何最小化jFrame?

[英]How to minimize a jFrame when the window close button is pressed?

I want to minimize the JFrame when the close button is pressed, I am using the code shown below, but whenever I press the close button, the frame first minimizes and then closes automatically.我想在按下关闭按钮时最小化 JFrame,我正在使用下面显示的代码,但是每当我按下关闭按钮时,框架首先最小化然后自动关闭。 I am working on Ubuntu.我在 Ubuntu 上工作。

How can I prevent the JFrame from closing?如何防止 JFrame 关闭?

public class MainClass {

    public static void main(String args[]) throws ClassNotFoundException {

        final Gui frame = new Gui();

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                frame.setExtendedState(Frame.ICONIFIED);
            }
        });

    }
}

Try maybe this way试试这种方式

final Gui frame = new Gui();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// <- prevent closing
frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        frame.setExtendedState(JFrame.ICONIFIED);
    }
});

frame.setSize(200,200);
frame.setVisible(true);

This will prevent window from closing, and your current code will minimize it (at least this is how it works on Win7, let me know if it helps or not).这将防止窗口关闭,并且您当前的代码会将其最小化(至少这是它在 Win7 上的工作方式,让我知道它是否有帮助)。

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

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