[英]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.