2015-11-02 88 views
2

嗨,这是java swing 1.7.0中提供的PopupFactory类的私有方法。无法理解Java 1.7 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的(这是摆在一个代码片段紧密结合,甚至一个组件(所有者) DesktopPane在JFrame中)在

popupType = HEAVY_WEIGHT_POPUP

它不与comment.Please相符的人解释这 结束 谢谢。

+2

我们没有写评论。你为什么在乎?你想解决什么问题? – camickr

+0

我想解决一个弹出式菜单中的绘画问题,这是不实际的带来我原来的问题我需要提供一个巨大的代码base.so我想了解java swing的行为,以获得一些线索。根据这个源代码,如果应用程序主窗口是JFrame,那么swing将总是产生大量的弹出窗口。但我觉得我错过了一些东西。 – hunter

+3

'把我原来的问题带到这里是不现实的,我需要提供一个庞大的代码库 - - 你需要创建一个[SSCCE](http://sscce.org/)来演示这个问题。 – camickr

回答

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。
+0

我接受这是正确的答案,我的问题请求解释关于评论。这第二点解释它。谢谢。 – hunter

+0

@hunter,谢谢,但是从你说过的话,现在听起来不像这种方法会成为你的问题。祝你好运。 – Linus