添加同一个对象两次
我有一个标签和两个面板。我想将标签添加到每个面板中,但渲染后仅显示一个标签。我可以创建第二个标签,但我认为,必须可以添加相同的标签两次。这是我的代码:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将相同的小部件添加到两个面板,一个小部件只能有一个父级。
解决方案很简单,创建具有相同内容的第二个标签。
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.
当您将一个小部件作为子部件添加到另一个小部件时,包含的类将首先执行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.