我刚开始使用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复合。事实上除了按钮之外什么都没有。因为它不是复合材料的一部分。
这是'下一步'复合材料:
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);
}
}
我做错了吗?我应该怎么做才能改变它?
答案 0 :(得分:4)
按如下方式更改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());