我想在一个销毁的Composite里再重新的放置新的控件。代码如下。
在Composite c 中加入了Composite c1和c2,他们里面有各自的button,b1和b2,我想达到的效果是,当点击button的时候,它所在的composite先被销毁,完了在重新加载上。public class DynamSample{
Display display;
Shell shell;
Composite c;
Composite c1;
        Composite c2;

public DynamSample(){
display = new Display();
shell = new Shell(display);
shell.setText("SWT Sample");
shell.setLayout( new FillLayout(SWT.VERTICAL));

c = new Composite(shell, SWT.NONE);
c.setLayout( new FillLayout() );

createGui1();
createGui2();

shell.open();
while (!shell.isDisposed())
      if (!display.readAndDispatch())
        display.sleep();

}

private void createGui1(){
c1 = new Composite(c, SWT.NONE);
c1.setLayout(new GridLayout(1, true));
Button b1 = new Button( c1, SWT.NONE);
b1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
b1.setText("Button 1");
b1.addSelectionListener(new SelectionListener(){
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
@Override
public void widgetSelected(SelectionEvent e) {
c1.dispose(); //先销毁了
createGui1(); //在重新加载上???但不成功

}});
}
private void createGui2(){
Composite c2 = new Composite(c, SWT.NONE);
c2.setLayout(new GridLayout( 1, true ));
Button b2 = new Button( c2, SWT.NONE);
b2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
b2.setText("Button 2");
b2.addSelectionListener(new SelectionListener(){
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
@Override
public void widgetSelected(SelectionEvent e) {
                        // 功能如上
}
});
}
public static void main(String args[]){
new DynamSample();
}
}问题是我点b1的后,c1被销毁, 但新的c1没有别重新加载上。不知道什么原因!!
我不知道是不是这个思路有问题,请各位高手帮帮忙!
多谢了!

解决方案 »

  1.   

    c1.dispose(); //先销毁了
    createGui1(); //在重新加载上???但不成功并不是添加在原来的位置,你可以在c1上套一层,而不是直接添加在c上,这样试试
      

  2.   

    我在c1上面又加了一个composite,然后把button加上面,但还是不行,只是销毁了,但重新加载不上。请问还有没有别的方法啊?
      

  3.   

    b1.addSelectionListener(new SelectionListener() {
    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
    } @Override
    public void widgetSelected(SelectionEvent e) {
    c1.dispose(); // 先销毁了
    createGui1(); // 在重新加载上???但不成功
    c.layout();//加上这一句话 }
    });