java 如何禁用 JFrame 中的关闭按钮?

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

How to disable close button in JFrame?

javaswingjframe

提问by Buggs

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

可能的重复:
如何禁用(或隐藏)JFrame 上的关闭 (x) 按钮?

I want the frame to have a deactivated close button, but I don't want to do frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); nor frame.setUndecorated(true).

我希望框架有一个停用的关闭按钮,但我不想这样做frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );也不frame.setUndecorated(true)

I want the frame to appear but the close button should look inactive/disabled.

我希望框架出现,但关闭按钮应该看起来不活动/禁用。

If we cant achieve this in JFrameis there a way to do this? You suggestions will be of great help.

如果我们不能做到这一点,JFrame有没有办法做到这一点?你的建议会有很大帮助。

回答by Dan D.

I think the best answer is that it is not possible.

我认为最好的答案是不可能。

I would choose the DO_NOTHING_ON_CLOSEand override the windowClosing event in its WindowAdapter as the best option.

我会选择DO_NOTHING_ON_CLOSE并覆盖其 WindowAdapter 中的 windowClosing 事件作为最佳选择。

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    // do whatever else
  }
});

回答by MD. Sahib Bin Mahboob

This bug reportsays that it is not possible , though it is a old link but it is not probably added in jdk yet :(

这个错误报告说这是不可能的,虽然它是一个旧链接,但它可能还没有添加到 jdk 中:(