如题,具体点说,也就是,我打开主窗体后,开了其他附属窗体,现在我想直接关闭主窗体,请问,如何使得能在关闭主窗体的同时,自动关闭这些附属窗体?程序应该如何实现?谢谢!
使用父窗体的shell 作为 子窗体的parent shell, 在你关闭父窗体时, 会自动关闭从这个父窗台弹出的所有子窗体.
主窗体的代码:
[code="java"]public class SwtDialog extends Dialog {
protected Object result;
protected Shell shell;
/**
* Create the dialog
* @param parent
*/
public SwtDialog(Shell parent) {
super(parent, SWT.NONE);
}
/**
* Open the dialog
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(500, 375);
shell.setText("SWT Dialog");
final Button button = new Button(shell, SWT.NONE);
button.setText("button");
button.setBounds(32, 31, 44, 23);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new PopUpDialog(shell).open();
// 注意这里,使用当前窗口的shell作为子窗台的parent 的shell.
}
});
}
}[/code]
子窗台的代码[一般代码]
[code="java"]public class PopUpDialog extends Dialog {
protected Object result;
protected Shell shell;
public PopUpDialog(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(500, 375);
shell.setText("SWT Dialog");
}
}[/code]