我想在鼠标点击事件中删除ContentPanel中的所有元素并添加新元素。它使用removeAll()方法正常工作,这将删除所有现有组件。但是当我想添加一个新组件时,它不会被添加。
答案 0 :(得分:2)
也许像这样的东西我省略了点击处理程序,但你应该从中得到这个想法。
private ContentPanel contentPanel;
public SwapScreen() {
contentPanel = new ContentPanel();
add(contentPanel);
}
public void swap1() {
/*This should be split into a separate
method and called only once to avoid recreating them.*/
field1 = new TextField<String>();
contentPanel.add(field1);
field2 = new TextField<String>();
contentPanel.add(field2);
this.layout(true);
}
public void swap2() {
/*This should be split into a separate
method and called only once to avoid recreating them.*/
anotherField1 = new TextField<String>();
contentPanel.add(anotherField1);
anotherField2 = new TextField<String>();
contentPanel.add(anotherField2);
this.layout(true);
}
最重要的部分是this.layout(true)
,以强制它刷新你的布局,