添加同一个对象两次

发布于 2024-08-19 22:04:43 字数 382 浏览 5 评论 0原文

我有一个标签和两个面板。我想将标签添加到每个面板中,但渲染后仅显示一个标签。我可以创建第二个标签,但我认为,必须可以添加相同的标签两次。这是我的代码:

// Create labels
Label sectorLabel = new Label("Bereich");

// Create panels/rows
HorizontalPanel row1 = new HorizontalPanel();
HorizontalPanel row2 = new HorizontalPanel();

// Add content to row1
row1.add(sectorLabel);

// Add content to row2      
row2.add(sectorLabel);

i have an label and two panels. i want to add the label into each panel, but after rendering is shown only one label. i can create second label, but i think, it must be possible to add the same label twice. Here my code:

// Create labels
Label sectorLabel = new Label("Bereich");

// Create panels/rows
HorizontalPanel row1 = new HorizontalPanel();
HorizontalPanel row2 = new HorizontalPanel();

// Add content to row1
row1.add(sectorLabel);

// Add content to row2      
row2.add(sectorLabel);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫言歌 2024-08-26 22:04:43

您不能将相同的小部件添加到两个面板,一个小部件只能有一个父级。

解决方案很简单,创建具有相同内容的第二个标签。

You can't add the same widget to two panels, a widget can have only one parent.

The solution is simply to create a second label with the same contents.

静谧幽蓝 2024-08-26 22:04:43

当您将一个小部件作为子部件添加到另一个小部件时,包含的类将首先执行removeFromParent 操作。所以不,你不能添加一个小部件两次。

你为什么要这么做?也许这是正确的问题。如果您不想更新屏幕上多个位置的状态,那么您可能需要对数据应用观察者模式,并在数据更改时更新所有观察者。

When you add a widget as a child to another widget, the containing class will first do a removeFromParent operation. So no you can not add a widget twice.

Why would you want to do that ? Maybe that is the right question to ask. If you don't want to update the status on multiple locations on the screen then maybe you need to apply an observer pattern to your data and update all the observers when the data changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文