SWT中怎样在消毁主窗口的时候,将自定义的Dialog也消毁?
如题.
代码如下:
//自定义的Dialog类:
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DialogA extends Dialog {
Display display;
static Shell shell;
public DialogA(Shell parent, int style) {
super(parent, style);
display = parent.getDisplay();
shell = new Shell(display);
shell.setText("This is Dialog");
}
//打开Dialog
public void open() {
shell.open();
while (!shell.isDisposed())
if (display.readAndDispatch())
display.sleep();
DialogTest.btn.setSelection(false);//将Dialog关闭时,使主窗口的按钮不选中.
shell.dispose();
}
//关闭Dialog
public void close() {
shell.dispose();
}
}
//测试的类:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DialogTest {
static DialogA dialog;
static Button btn;
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
shell.setText("Main Shell");
btn = new Button(shell, SWT.CHECK);
btn.setText("TEST");
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (btn.getSelection() == true) {
dialog = new DialogA(shell, SWT.NONE);
dialog.open();
} else {
dialog.close();
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
if (display.readAndDispatch())
display.sleep();
dialog.close();//此处在关闭主窗口的时候,关闭Dialog.但不起作用.
display.dispose();
}
}