【问题标题】:Why JFrame redefine the "EXIT_ON_CLOSE" which it inherits from interface "WindowConstants"?为什么JFrame重新定义它从接口“WindowConstants”继承的“EXIT_ON_CLOSE”?
【发布时间】:2014-12-08 12:40:56
【问题描述】:

WindowConstants 是这样定义的:

public interface WindowConstants
{
    public static final int DO_NOTHING_ON_CLOSE = 0;

    public static final int HIDE_ON_CLOSE = 1;

    public static final int DISPOSE_ON_CLOSE = 2;

    public static final int EXIT_ON_CLOSE = 3;

}

JFrame 是这样定义的:

public class JFrame  extends Frame implements WindowConstants,
                                              Accessible,
                                              RootPaneContainer,
                              TransferHandler.HasGetTransferHandler
{
    /**
     * The exit application default window close operation. If a window
     * has this set as the close operation and is closed in an applet,
     * a <code>SecurityException</code> may be thrown.
     * It is recommended you only use this in an application.
     * <p>
     * @since 1.3
     */
    public static final int EXIT_ON_CLOSE = 3;

为什么要重新定义EXIT_ON_CLOSE?而且既然是WindowConstants接口中的final,那怎么重新定义呢?

【问题讨论】:

  • 为什么需要重新定义 EXIT_ON_CLOSE?
  • 不,我没有重新定义它。而且我认为没有必要。但它是在 JFrame 中定义的,如源代码所示。我只是想知道为什么。
  • 因为 API 中默认有 HIDE_ON_CLOSE,所以 EXIT_ON_CLOSE 没有 :-)
  • 在 Java5/6 中更改/从 Java5/6(最新 5 和新 6 版本)
  • @mKorbel 我看到JFrame 中有一个private int defaultCloseOperation = HIDE_ON_CLOSE;。但我仍然不明白为什么在JFrame 中重新定义了EXIT_ON_CLOSE

标签: java swing jframe windowlistener


【解决方案1】:

在 Java 1.3 中,当所有这些都被添加时,EXIT_ON_CLOSE 仅与 JFrame 相关,与 WindowConstants 的其他实现无关。因此 - 它出现在 WindowConstants 中,并在 JFrame 中定义。其他 3 个 XXX_ON_CLOSE 选项在界面中。 (英文 Javadoc 不再在线,但仍可下载,因此此处不提供参考。如果您搜索“WindowConstants Java 1.3”,您将获得 Javadoc 的日文版本 - 但由于页面结构相同,您仍然可以看到重点)

后来 (1.4) 移至 WindowConstants,但由于兼容性问题,该字段并未从 JFrame 中删除。

那里没有重新定义。正在发生的事情是shadowing。 IE。 JFrame 字段隐藏(但不消除)WindowConstants 字段。

【讨论】:

猜你喜欢
  • 2012-04-15
  • 2012-01-21
  • 2016-09-12
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
相关资源
最近更新 更多