WEB开发网
开发学院WEB开发Jsp 实战SWT布局 阅读

实战SWT布局

 2008-01-05 09:12:13 来源:WEB开发网   
核心提示:标准的SWT布局类FillLayout:在容器中以相同的大小单行或单列的排列组件RowLayout:以单行或多行的方式使用几个选项(fill,wrap,spacing,justify,type)定制组件的排列方式GridLayout:类似于swing的GridLayout的方式以格子的方式排列组件FormLayout(

  标准的SWT布局类
FillLayout:在容器中以相同的大小单行或单列的排列组件
RowLayout:以单行或多行的方式使用几个选项(fill,wrap,spacing,justify,type)定制组件的排列方式
GridLayout:类似于swing的GridLayout的方式以格子的方式排列组件
FormLayout(SWT 2.0的新特性):通过定义组件四个边的“粘贴”位置来排列组件,被引用的相对的组件可以父组件,也可以是同一容器中的其它组件。
在SWT中,可以由用户自定义布局类。

在简单的布局中,使用FillLayout和RowLayout也许就够用了,也比较简单。但是通常的布局都比较复杂,而且要求很精确。无论复杂程度如何,都可以由GridLayout或FormLayout来完成。通常GridLayout与FormLayout可以做成同样的效果,但是使用FormLayout更有效,不会像GridLayout产生resize导致的布局错位,也更简单。下面通过一个例子简单介绍FormLayout的使用。

布局效果


布局实施
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);

完整的源码
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.com.efly.clientframe.core.Hook;

/**
* @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);

shell.pack();
Rectangle shellBounds = shell.getBounds();
shell.setLocation(
(clientArea.width - shellBounds.width) / 2,
(clientArea.height - shellBounds.height) / 2);

shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

private void dispose() {
display.dispose();

}

private void cancel() {
dispose();
}

private void ok() {
verify();
}

private void verify() {
rootHook.runPlatform();
}

public static void main(String[]){
new LoginUI(null).show();
}
}

Tags:实战 SWT 布局

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接