无法理解Java 1.7 PopupFactory源代码

时间:2015-11-02 14:38:31

标签: java swing

嗨,这是java swing 1.7.0中提供的PopupFactory类的私有方法。

/**
 * Returns the popup type to use for the specified parameters.
 */
private int getPopupType(Component owner, Component contents,
                         int ownerX, int ownerY) {
    int popupType = getPopupType();

    if (owner == null || invokerInHeavyWeightPopup(owner)) {
        popupType = HEAVY_WEIGHT_POPUP;
    }
    else if (popupType == LIGHT_WEIGHT_POPUP &&
             !(contents instanceof JToolTip) &&
             !(contents instanceof JPopupMenu)) {
        popupType = MEDIUM_WEIGHT_POPUP;
    }

    // Check if the parent component is an option pane.  If so we need to
    // force a heavy weight popup in order to have event dispatching work
    // correctly.
    Component c = owner;
    while (c != null) {
        if (c instanceof JComponent) {
            if (((JComponent)c).getClientProperty(
                        PopupFactory_FORCE_HEAVYWEIGHT_POPUP) ==   Boolean.TRUE) {
                popupType = HEAVY_WEIGHT_POPUP;
                break;
            }
        } else if (c instanceof Window) {
            Window w = (Window) c;
            if (!w.isOpaque() || w.getOpacity() < 1 || w.getShape() != null)  {
                popupType = HEAVY_WEIGHT_POPUP;
                break;
            }
        }
        c = c.getParent();
    }

    return popupType;
}

我的问题是,在评论中说,

    // Check if the parent component is an option pane.  If so we need to
    // force a heavy weight popup in order to have event dispatching work
    // correctly.

但是当我仔细查看代码片段时,即使放置在JInternalFrame中的组件(所有者)(放在JFrame中的DesktopPane中)也会在

中结束。

popupType = HEAVY_WEIGHT_POPUP

它与评论不符。请有人解释一下 谢谢。

1 个答案:

答案 0 :(得分:3)

我找到了Java 8源代码中的相应部分。方法的行为发生了很小的变化:

// Check if the parent component is an option pane.  If so we need to
// force a heavy weight popup in order to have event dispatching work
// correctly.
Component c = owner;
while (c != null) {
    if (c instanceof JComponent) {
        if (((JComponent)c).getClientProperty(
                    PopupFactory_FORCE_HEAVYWEIGHT_POPUP) == Boolean.TRUE) {
            popupType = HEAVY_WEIGHT_POPUP;
            break;
        }
    }
    c = c.getParent();
}

所以,如果我可以推测,

  1. 评论是先前版本的副产品,并没有描述现在的工作方式。 注意他们改变了Java 8的方法,但评论仍然是逐字的。
  2. 该方法被编写为可扩展为更多类型,但最初主要是“选项窗格”将PopupFactory_FORCE_HEAVYWEIGHT_POPUP设置为true。