您可以将SWT.Deactivate侦听器附加到对话框的基础Shell.
要附加侦听器,您可以覆盖Window :: configureShell,如下所示:
@Override
protected void configureShell( Shell shell ) {
super.configureShell( shell );
shell.addListener( SWT.Deactivate, event -> shell.close() );
}
这里有一个独立的SWT示例来说明裸机制:
Display display = new Display();
Shell parentShell = new Shell(display);
parentShell.setSize(500, 500);
parentShell.open();
Shell shell = new Shell(parentShell);
shell.addListener(SWT.Deactivate, event -> shell.close());
shell.setSize(300, 300);
shell.setText("Closes on Deactivate");
shell.open();
while (!parentShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();