将相同的小部件添加到两个面板导致问题

我已经创建了两个VerticalPanel(mainVP,subVP)和一个TextBox(框).TextBox(框)被添加到mainVP以及subVP但是我只在这里添加mainVP到RootPanel如果我lauch我的应用程序没有任何东西是可见的,即使我有将TextBox(框)添加到VerticalPanel(mainVP),并添加到主Rootpanel。

VerticalPanel mainVP=new VerticalPanel(); VerticalPanel subVP=new VerticalPanel(); TextBox box=new TextBox(); mainVP.add(box); //Textbox added to VerticalPanel subVP.add(box); RootPanel.get().add(mainVP);//mainVP contains TextBox 

任何人都可以解释一下上面的代码是如何在内部工作的吗?

您的面板子subVP不会添加到任何内容中,当该box添加到subVP ,它将从mainVP删除。 小部件一次只能是一个位置。

(为发布的评论添加更多细节)

这是小部件如何工作的基本假设的一部分 – 它不是可以在几个地方放在页面上的“标记”,而是代表一个或多个DOM元素,并将它们包装起来以便于使用和重用。 每个窗口小部件都会公开一些您可以监听的事件处理程序,以及更改窗口小部件外观的方法。

想象一下,如果你在页面上有两个按钮,你需要他们做同样的事情。 在查看页面时,显然有两个按钮,虽然它们看起来相同并导致相同的动作,但它们并不是同一个东西。

考虑按钮在内部如何工作 – 让我们说它检查鼠标是否hover,如果是,则显示工具提示或更改颜色。 如果在两个位置呈现相同的窗口小部件,则现在都将进行此更改。

从API方面看: Widget有一个方法getParent() ,它返回父窗口小部件。 如果您可以一次将一个小部件添加到多个位置,则无法使用getParent() ,或者需要返回一个列表。

Panel同样具有indexOf ,但是如果你可以将一个小部件添加到多个父级,那么你也可以多次将同一个小部件添加到同一个父级 – indexOf返回什么?

最后,实现这一目标。 从Panel.add的Javadoc:

 /** * Adds a child widget. * * 

* How to Override this Method *

*

* There are several important things that must take place in the correct * order to properly add or insert a Widget to a Panel. Not all of these steps * will be relevant to every Panel, but all of the steps must be considered. *

    *
  1. Validate: Perform any sanity checks to ensure the Panel can * accept a new Widget. Examples: checking for a valid index on insertion; * checking that the Panel is not full if there is a max capacity.
  2. *
  3. Adjust for Reinsertion: Some Panels need to handle the case * where the Widget is already a child of this Panel. Example: when performing * a reinsert, the index might need to be adjusted to account for the Widget's * removal. See {@link ComplexPanel#adjustIndex(Widget, int)}.
  4. *
  5. Detach Child: Remove the Widget from its existing parent, if * any. Most Panels will simply call {@link Widget#removeFromParent()} on the * Widget.
  6. *
  7. Logical Attach: Any state variables of the Panel should be * updated to reflect the addition of the new Widget. Example: the Widget is * added to the Panel's {@link WidgetCollection} at the appropriate index.
  8. *
  9. Physical Attach: The Widget's Element must be physically * attached to the Panel's Element, either directly or indirectly.
  10. *
  11. Adopt: Call {@link #adopt(Widget)} to finalize the add as the * very last step.
  12. *
*

* * @param child the widget to be added * @throws UnsupportedOperationException if this method is not supported (most * often this means that a specific overload must be called) * @see HasWidgets#add(Widget) */ public void add(Widget child)

最后,GWT中大多数面板使用的默认实现基本上都是这个( ComplexPanel.add ):

 // Detach new child. child.removeFromParent(); // Logical attach. getChildren().add(child); // Physical attach. DOM.appendChild(container, child.getElement()); // Adopt. adopt(child); 

还有其他实现,但它们大多归结为此,符合Panel概述的指导原则。