我想在按下关闭按钮时最小化 JFrame,我正在使用下面显示的代码,但是每当我按下关闭按钮时,框架首先最小化然后自动关闭。我正在使用 Ubuntu。
如何防止 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);
}
});
}
}
最佳答案
试试这个方法
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);
这将防止窗口关闭,并且您当前的代码会将其最小化(至少这是它在 Win7 上的工作方式,如果有帮助请告诉我)。
关于java - 如何在按下窗口关闭按钮时最小化 jFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17794322/