[img]http://dl.iteye.com/upload/attachment/0065/7100/4528c09a-42fd-3efa-b11a-a070006648fc.png[/img]
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Text;
/**
* 测试父组件被释放,子组件或子窗口也将被释放
* display为shell的父组件
* shell可以为其他shell的父组件
* 测试打印屏幕尺寸的方法
*
*/
public class testShellTree {
private Text text;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
testShellTree window = new testShellTree();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
//父窗口
Shell shellParent = new Shell();
shellParent.setSize(602, 300);
shellParent.setText("Parent shell, close this one will close the child window, too");
//释放display的按钮
Button btn = new Button(shellParent, SWT.NONE);
btn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Display.getDefault().dispose();
}
});
btn.setBounds(10, 10, 193, 27);
btn.setToolTipText("关闭display对象后,其所有子shell都将被关闭");
btn.setText("click me to dispose the Display");
//显示屏幕信息的文本框
text = new Text(shellParent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
text.setBounds(28, 91, 400, 127);
String monitorInfo=Display.getDefault().getPrimaryMonitor().getBounds().toString();
String clientAreaInfo=Display.getDefault().getPrimaryMonitor().getClientArea().toString();
text.setText("屏幕大小为:"+monitorInfo+"\n用户区域大小为(去掉了任务栏的大小):"+clientAreaInfo);
//子窗口
Shell shellChild=new Shell(shellParent);
shellChild.setText("Child, can be closed independently");
shellChild.setSize(400, 300);
Button btn2 = new Button(shellChild, SWT.NONE);
btn2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Display.getDefault().dispose();
}
});
btn2.setToolTipText("关闭display对象后,其所有子shell都将被关闭");
btn2.setBounds(10, 117, 193, 27);
btn2.setText("click me to dispose the Display");
shellChild.setLocation(shellParent.getLocation().x+400, shellParent.getLocation().y+200);
shellParent.open();
shellChild.open();
shellParent.layout();
//事件循环
while (!shellParent.isDisposed()||!shellChild.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}