布局实施 1.首先定义窗口和它的空白边 Display.getDefault().dispose(); //移去平台核心启动画面 display = new Display(); shell = new Shell(display, SWT.TITLE); FormLayout layout = new FormLayout(); layout.marginHeight = 10; layout.marginWidth = 20; shell.setLayout(layout); shell.setText("用户登录");
2.创建窗口上的元素,其中下面的两个button由一个使用RowLayout的组件来包容。 name = new Label(shell, SWT.NONE); name.setText("用户名"); nameText = new Text(shell, SWT.SINGLE SWT.BORDER); pass = new Label(shell, SWT.NONE); pass.setText("密码"); passText = new Text(shell, SWT.SINGLE SWT.BORDER); passText.setEchoChar(´*´); passText.setTabs(2); bottom = new Composite(shell, SWT.NONE); RowLayout rowLayout = new RowLayout(); rowLayout.justify = true; //justify定义组件在容器中分散开,效果类似于swing的FlowLayout bottom.setLayout(rowLayout);
3.定义name标签的位置。它的顶边离父组件(窗口shell)的空白边距离是父组件clientArea(除空白边以外的空间)高度(height)的15%,偏移的点数(points)为0。 FormData data = new FormData(); data.top = new FormAttachment(15, 0); name.setLayoutData(data);
4.定义name文本输入的位置。它的顶边在name标签的中心位置(这不是正确的表达,但程序是这样解释,事实上它的中心位置与name标签在同一条水平线上),左边距name标签的右边有10个点。 data = new FormData(); data.top = new FormAttachment(name, 0, SWT.CENTER); data.left = new FormAttachment(name, 10, SWT.RIGHT); nameText.setLayoutData(data);
5.定义pass标签的位置。它的顶边距name标签的底边有10个点数的偏移。 data = new FormData(); data.top = new FormAttachment(name, 10, SWT.BOTTOM); pass.setLayoutData(data);
6.定义pass文本输入的位置。它的顶边在name标签的中心位置(同上),左边与name文本框的左边对齐。 data = new FormData(); data.top = new FormAttachment(pass, 0, SWT.CENTER); data.left = new FormAttachment(nameText, 0, SWT.LEFT); passText.setLayoutData(data);
7.定义bottom组件的位置。它的顶边距pass标签的底边15个点数,左边与pass标签的左边对齐,右边与pass文本输入的右边对齐。 data = new FormData(); data.top = new FormAttachment(pass, 15, SWT.BOTTOM); data.left = new FormAttachment(pass, 0, SWT.LEFT); data.right = new FormAttachment(passText, 0, SWT.RIGHT); bottom.setLayoutData(data);
/** * @author efly * @version 1.0.0,11/22/02 */ public final class LoginUI { PRivate Display display; private Shell shell; private Composite bottom; private Label name; private Label pass; private Text nameText; private Text passText; private Button ok; private Button exit; private Rectangle clientArea; private RootHook rootHook;
public LoginUI(Hook hook) { rootHook = (RootHook) hook; }
/** * 显示登陆界面 */ public void show() { Display.getDefault().dispose(); display = new Display(); clientArea = display.getClientArea(); shell = new Shell(display, SWT.TITLE); FormLayout layout = new FormLayout(); layout.marginHeight = 10; layout.marginWidth = 20; shell.setLayout(layout); shell.setText("用户登录");
name = new Label(shell, SWT.NONE); name.setText("用户名"); nameText = new Text(shell, SWT.SINGLE SWT.BORDER); pass = new Label(shell, SWT.NONE); pass.setText("密码"); passText = new Text(shell, SWT.SINGLE SWT.BORDER); passText.setEchoChar(´*´); passText.setTabs(2); bottom = new Composite(shell, SWT.NONE); RowLayout rowLayout = new RowLayout(); rowLayout.justify = true; bottom.setLayout(rowLayout); ok = new Button(bottom, SWT.PUSH); ok.setText("确定"); ok.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { ok(); } }); exit = new Button(bottom, SWT.PUSH); exit.setText("取消"); exit.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { cancel(); } });
FormData data = new FormData(); data.top = new FormAttachment(15, 0); name.setLayoutData(data);
data = new FormData(); data.top = new FormAttachment(name, 0, SWT.CENTER); data.left = new FormAttachment(name, 10, SWT.RIGHT); nameText.setLayoutData(data);
data = new FormData(); data.top = new FormAttachment(name, 10, SWT.BOTTOM); pass.setLayoutData(data);
data = new FormData(); data.top = new FormAttachment(pass, 0, SWT.CENTER); data.left = new FormAttachment(nameText, 0, SWT.LEFT); passText.setLayoutData(data);
data = new FormData(); data.top = new FormAttachment(pass, 15, SWT.BOTTOM); data.left = new FormAttachment(pass, 0, SWT.LEFT); data.right = new FormAttachment(passText, 0, SWT.RIGHT); bottom.setLayoutData(data);