Java 如何禁用(或隐藏)JFrame 上的关闭 (x) 按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/276254/
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-11 12:23:26  来源:igfitidea点击:

How to disable (or hide) the close (x) button on a JFrame?

javawindowsswing

提问by florin

I have a window (derived from JFrame) and I want to disable the close button during certain operations which are not interruptible. I know I can make the button not do anything (or call a handler in a WindowListener) by calling

我有一个窗口(派生自 JFrame),我想在某些不可中断的操作中禁用关闭按钮。我知道我可以通过调用使按钮不执行任何操作(或调用 WindowListener 中的处理程序)

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

but I would like to make it clear visually that it is pointless to click it.

但我想在视觉上清楚地表明点击它是没有意义的。

采纳答案by bdumitriu

If I understand it correctly, this bug reportindicates that this is currently not possible.

如果我理解正确,这个错误报告表明这目前是不可能的。

回答by Malfist

This is probably the best you are going to get:

这可能是您将得到的最好的结果:

setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.NONE);

This will remove the entire titlebar, java doesn't really specify a way to remove individual components of the titlebar

这将删除整个标题栏,java 并没有真正指定删除标题栏各个组件的方法

edit:

编辑:

There may be a way, check out these threads:

可能有一种方法,请查看这些线程:

回答by Spencer Kormos

For those coming to this later than 2008, there has been a change making it possible to do this. See this link

对于那些在 2008 年之后加入的人来说,已经发生了变化,可以做到这一点。看这个链接

Second response from the bottom shows how to do it by name.

底部的第二个响应显示了如何按名称进行操作。

回答by Ab Hin

Please try this

请试试这个

frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        e.getWindow().setVisible(false);
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(WindowsActions.class.getName()).log(Level.SEVERE, null, ex);
            }
      }
    });

回答by V?r?s Richárd

This will help you:

这将帮助您:

frame.setDefaultCloseOperation(0);

回答by Cancer000

To simply make them disappear, try the following:

要简单地使它们消失,请尝试以下操作:

setUndecorated(true);