简体   繁体   English

在eclipse swt中切换复合材料

[英]Switch between composites in eclipse swt

I just started using eclipse window builder. 我刚开始使用eclipse窗口构建器。 I want to switch between two composites when I click a button. 当我点击一个按钮时,我想在两个复合体之间切换。 This is what I have in my main window : 这就是我在主窗口中的内容:

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");

}
}

The composite I am trying to change to is the one called 'Next' in the above code. 我试图改变的复合是上面代码中的'Next'。 But when I click on the button, the current composite gets removed because of the dispose function but the Next composite isnt seen. 但是当我单击按钮时,由于dispose函数而导致当前复合被删除,但是看不到Next复合。 In fact nothing is there at all except the button. 事实上除了按钮之外什么都没有。 Since its not a part of the composite. 因为它不是复合材料的一部分。

This is the 'Next' composite : 这是'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);

}
}

Am I doing something wrong? 难道我做错了什么? What should I do to change it? 我应该怎么做才能改变它?

Change the Next class as follows: 更改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);
     }
 }

Add the below line in btnNewButton listener ie in widgetSelected method btnNewButton监听器中添加以下行,即在widgetSelected方法中

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

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

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