请问,SWT中,如何实现在当前窗口中打开一个新窗口?由于刚接触SWT,还不懂改如何实现,谢谢!
需要给控件添加事件监听,在事件执行中就可以去写打开另一个Dialog的代码了.
[code="java"] final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new CustomDialog(Display.getCurrent().getActiveShell()).open();
}
}); [/code]
[code="java"]import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SwtApplication {
protected Shell shell;
public static void main(String[] args) throws Exception {
SwtApplication window = new SwtApplication();
window.open();
}
public void open() {
final 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(500, 375);
shell.setText("SWT");
final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new CustomDialog(Display.getCurrent().getActiveShell()).open();
}
});
button.setText("button");
button.setBounds(438, 308, 44, 23);
}
}[/code]
[code="java"]import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CustomDialog extends Dialog {
protected Object result;
protected Shell shell;
public CustomDialog(Shell parent) {
super(parent, SWT.NONE);
}
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(312, 212);
shell.setText("SWT Dialog");
final Button button = new Button(shell, SWT.NONE);
button.setText("button");
button.setBounds(127, 74, 44, 23);
}
}[/code]