SWT笔记1,组件的关系,屏幕信息

9 篇文章 0 订阅
[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();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值