java - 看不懂Java 1.7 PopupFactory源码

标签 java swing

您好,这是 java swing 1.7.0 中提供的 PopupFactory 类的私有(private)方法。

/**
 * 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

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

最佳答案

我在 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. 该方法被编写为可扩展到更多类型,但最初且主要是“选项 Pane ”会将 PopupFactory_FORCE_HEAVYWEIGHT_POPUP 设置为 true。

关于java - 看不懂Java 1.7 PopupFactory源码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33480113/

相关文章:

用于计算德州扑克牌赢率的 Java 库

java - 内容更新后重新对齐左侧参差不齐的 SWT TableEditor

java - 在Java MapReduce中,Reducer的可迭代值似乎不一致

java - 如何让 JCheckBox 按我想要的方式工作

java - 一个字段上的两种搜索?

java - 如何连接到 AWS Elasticsearch?

java项目监听器

java - 解决代码中的错误

java - 关于Java SWING和Swing Application Framework的一些疑惑

java - 当鼠标光标移动到角落时如何显示条形?