0

我想在鼠标单击事件中从 ContentPanel 中删除所有元素并添加新元素。使用 removeAll() 方法可以正常工作,这会删除所有现有组件。但是当我想添加一个新组件时,它不会被添加。

4

1 回答 1

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)强制它刷新你的布局,

于 2012-05-09T09:45:01.120 回答