Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将相同的小部件添加到两个面板导致问题_Java_Gwt - Fatal编程技术网

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

Java 将相同的小部件添加到两个面板导致问题,java,gwt,Java,Gwt,我已经创建了两个VerticalPanel(mainVP,subVP)和一个TextBox(box)。TextBox(box)被添加到mainVP和subVP中,但是如果我启动我的应用程序,我只将mainVP添加到RootPanel中,即使我已经将TextBox(box)添加到VerticalPanel(mainVP)中,但没有任何内容可见 VerticalPanel mainVP=new VerticalPanel(); VerticalPanel subVP=new VerticalPa

我已经创建了两个VerticalPanel(mainVP,subVP)和一个TextBox(box)。TextBox(box)被添加到mainVP和subVP中,但是如果我启动我的应用程序,我只将mainVP添加到RootPanel中,即使我已经将TextBox(box)添加到VerticalPanel(mainVP)中,但没有任何内容可见

 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
没有添加到任何内容,当
框添加到
subVP
时,它将从
mainp
中删除。小部件一次只能位于一个位置

--

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

这是小部件工作原理的基本假设的一部分——它不是可以放在页面上多个位置的“戳记”,而是表示一个或多个DOM元素,并将它们包装起来以便于使用和重用。每个小部件都公开了一些您可以侦听的事件处理程序,以及更改小部件外观的方法

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

考虑一下按钮在内部是如何工作的——假设它有一个检查,看看鼠标是否悬停,如果是的话,显示一个工具提示,或者改变颜色。如果同一个小部件在两个地方呈现,那么现在两个地方都将得到此更改

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

面板同样具有
indexOf
,但是如果您可以将一个小部件添加到多个父级,那么您也可以将同一个小部件添加到同一个父级多次-那么
indexOf
会返回什么

最后,实现这一点。从Javadoc for
面板添加:

/**
 * Adds a child widget.
 * 
 * <p>
 * <b>How to Override this Method</b>
 * </p>
 * <p>
 * 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.
 * <ol>
 * <li><b>Validate:</b> 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.</li>
 * <li><b>Adjust for Reinsertion:</b> 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)}.</li>
 * <li><b>Detach Child:</b> Remove the Widget from its existing parent, if
 * any. Most Panels will simply call {@link Widget#removeFromParent()} on the
 * Widget.</li>
 * <li><b>Logical Attach:</b> 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.</li>
 * <li><b>Physical Attach:</b> The Widget's Element must be physically
 * attached to the Panel's Element, either directly or indirectly.</li>
 * <li><b>Adopt:</b> Call {@link #adopt(Widget)} to finalize the add as the
 * very last step.</li>
 * </ol>
 * </p>
 * 
 * @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)
// Detach new child.
child.removeFromParent();

// Logical attach.
getChildren().add(child);

// Physical attach.
DOM.appendChild(container, child.getElement());

// Adopt.
adopt(child);

还有其他的实现,但它们大多归结为这一点,根据
面板

中概述的指导原则,您的面板
subVP
没有添加到任何内容中,当
添加到
subVP
时,它将从
mainp
中删除。小部件一次只能位于一个位置

--

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

这是小部件工作原理的基本假设的一部分——它不是可以放在页面上多个位置的“戳记”,而是表示一个或多个DOM元素,并将它们包装起来以便于使用和重用。每个小部件都公开了一些您可以侦听的事件处理程序,以及更改小部件外观的方法

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

考虑一下按钮在内部是如何工作的——假设它有一个检查,看看鼠标是否悬停,如果是的话,显示一个工具提示,或者改变颜色。如果同一个小部件在两个地方呈现,那么现在两个地方都将得到此更改

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

面板同样具有
indexOf
,但是如果您可以将一个小部件添加到多个父级,那么您也可以将同一个小部件添加到同一个父级多次-那么
indexOf
会返回什么

最后,实现这一点。从Javadoc for
面板添加:

/**
 * Adds a child widget.
 * 
 * <p>
 * <b>How to Override this Method</b>
 * </p>
 * <p>
 * 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.
 * <ol>
 * <li><b>Validate:</b> 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.</li>
 * <li><b>Adjust for Reinsertion:</b> 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)}.</li>
 * <li><b>Detach Child:</b> Remove the Widget from its existing parent, if
 * any. Most Panels will simply call {@link Widget#removeFromParent()} on the
 * Widget.</li>
 * <li><b>Logical Attach:</b> 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.</li>
 * <li><b>Physical Attach:</b> The Widget's Element must be physically
 * attached to the Panel's Element, either directly or indirectly.</li>
 * <li><b>Adopt:</b> Call {@link #adopt(Widget)} to finalize the add as the
 * very last step.</li>
 * </ol>
 * </p>
 * 
 * @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)
// Detach new child.
child.removeFromParent();

// Logical attach.
getChildren().add(child);

// Physical attach.
DOM.appendChild(container, child.getElement());

// Adopt.
adopt(child);

还有其他的实现,但它们大多归结为这一点,根据
面板

中概述的指导原则,您的面板
subVP
没有添加到任何内容中,当
添加到
subVP
时,它将从
mainp
中删除。小部件一次只能位于一个位置

--

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

这是小部件工作原理的基本假设的一部分——它不是可以放在页面上多个位置的“戳记”,而是表示一个或多个DOM元素,并将它们包装起来以便于使用和重用。每个小部件都公开了一些您可以侦听的事件处理程序,以及更改小部件外观的方法

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

考虑一下按钮在内部是如何工作的——假设它有一个检查,看看鼠标是否悬停,如果是的话,显示一个工具提示,或者改变颜色。如果同一个小部件在两个地方呈现,那么现在两个地方都将得到此更改

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

面板同样具有
indexOf
,但是如果您可以将一个小部件添加到多个父级,那么您也可以将同一个小部件添加到同一个父级多次-那么
indexOf
会返回什么

最后,实现这一点。从Javadoc for
面板添加:

/**
 * Adds a child widget.
 * 
 * <p>
 * <b>How to Override this Method</b>
 * </p>
 * <p>
 * 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.
 * <ol>
 * <li><b>Validate:</b> 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.</li>
 * <li><b>Adjust for Reinsertion:</b> 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)}.</li>
 * <li><b>Detach Child:</b> Remove the Widget from its existing parent, if
 * any. Most Panels will simply call {@link Widget#removeFromParent()} on the
 * Widget.</li>
 * <li><b>Logical Attach:</b> 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.</li>
 * <li><b>Physical Attach:</b> The Widget's Element must be physically
 * attached to the Panel's Element, either directly or indirectly.</li>
 * <li><b>Adopt:</b> Call {@link #adopt(Widget)} to finalize the add as the
 * very last step.</li>
 * </ol>
 * </p>
 * 
 * @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)
// Detach new child.
child.removeFromParent();

// Logical attach.
getChildren().add(child);

// Physical attach.
DOM.appendChild(container, child.getElement());

// Adopt.
adopt(child);

还有其他的实现,但它们大多归结为这一点,与
面板

中概述的指导原则一致,您的面板
子VP
没有添加到任何内容中,当
添加到
子VP
中时,它将