为什么 JFrame 重新定义了“EXIT_ON_CLOSE"?它从接口“WindowConstants"继承?

2022-01-24 00:00:00 java swing jframe windowlistener

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,那怎么重新定义呢?

Why the EXIT_ON_CLOSE is redefined? And since it is final in the WindowConstants interface, how could it be redefined?

推荐答案

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

In Java 1.3, when this was all added, EXIT_ON_CLOSE was relevant only to JFrame and not to other implementations of WindowConstants. As such - it was not present in WindowConstants and was defined in JFrame. The 3 other XXX_ON_CLOSE options were in the interface. (English Javadoc is not online anymore, though still downloadable, so no reference here. If you search for "WindowConstants Java 1.3" you'll get a Japanese version of the Javadoc - but since the page structure is the same, you can still see the point)

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

It was later (1.4) moved to WindowConstants, however the field was not removed from JFrame due to compatibility issues.

那里没有重新定义.发生了什么是 阴影.IE.JFrame 字段隐藏(但不消除)WindowConstants 字段.

There is no redefining there. What's happening is shadowing. I.e. the JFrame field hides (but does not eliminate) the WindowConstants field.

相关文章