繁体   English   中英

在eclipse swt中切换复合材料

[英]Switch between composites in eclipse swt

我刚开始使用eclipse窗口构建器。 当我点击一个按钮时,我想在两个复合体之间切换。 这就是我在主窗口中的内容:

public class DashboardView {

protected Shell shell;
private Composite composite;
private Next next;
private FormLayout layout;

public static void main(String[] args) {
    try {
        DashboardView window = new DashboardView();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

protected void createContents() {
    shell = new Shell();
    shell.setSize(572, 390);
    shell.setText("SWT Application");
    layout = new FormLayout();
    shell.setLayout(layout);

    composite = new Composite(shell, SWT.NONE);
    composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    FormData fd_composite_1 = new FormData();
    fd_composite_1.top = new FormAttachment(0, 10);
    fd_composite_1.left = new FormAttachment(0, 10);
    fd_composite_1.bottom = new FormAttachment(0, 299);
    fd_composite_1.right = new FormAttachment(0, 546);
    composite.setLayoutData(fd_composite_1);

    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            composite.dispose();
            next = new Next(shell, SWT.NONE);
        }
    });
    FormData fd_btnNewButton = new FormData();
    fd_btnNewButton.bottom = new FormAttachment(100, -10);
    fd_btnNewButton.right = new FormAttachment(composite, 0, SWT.RIGHT);
    btnNewButton.setLayoutData(fd_btnNewButton);
    btnNewButton.setText("New Button");

}
}

我试图改变的复合是上面代码中的'Next'。 但是当我单击按钮时,由于dispose函数而导致当前复合被删除,但是看不到Next复合。 事实上除了按钮之外什么都没有。 因为它不是复合材料的一部分。

这是'Next'复合材料:

public class Next extends Composite {
private Text txtYouAreIn;
public Next(Composite parent, int style) {
    super(parent, style);

    Button btnYay = new Button(this, SWT.NONE);
    btnYay.setBounds(43, 58, 75, 25);
    btnYay.setText("Yay");

    txtYouAreIn = new Text(this, SWT.BORDER);
    txtYouAreIn.setText("You are in a new");
    txtYouAreIn.setBounds(173, 113, 115, 21);

}
}

难道我做错了什么? 我应该怎么做才能改变它?

更改Next类如下:

 public class Next extends Composite {
     private Text txtYouAreIn;
     public Next(Composite parent, int style) {
        super(parent, style);
         Button btnYay = new Button(parent, SWT.NONE);
         btnYay.setBounds(43, 58, 75, 25);
         btnYay.setText("Yay");

         txtYouAreIn = new Text(parent, SWT.BORDER);
         txtYouAreIn.setText("You are in a new");
         txtYouAreIn.setBounds(173, 113, 115, 21);
     }
 }

btnNewButton监听器中添加以下行,即在widgetSelected方法中

 composite.setVisible(!composite.isVisible());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM