定制布局(Custom layouts)
Eclipse Forms在SWT layout的基础上增加了两个新的layout,这些layout继承了SWT Layout基类并能够在任何SWT composite上使用,但是一般都是和Eclipse Forms联合使用的。
TableWrapLayout
现在我们知道如何来组合一个form,让我们先给一个悬念.我们会改变那个超链接文本使它加长一些:
link.setText("This is an example of a form that is much longer "
"and will need to wrap.");
让我们看看结果:
图片7:一个使用GridLayout的form
发生什么事了?记住我们使用的是GridLayout,当它问这个超链接组件来计算它大小时,超链接告诉它文字在单独行里需要的长度,虽然我们告诉组件去包裹(wrap),它并没有这样做因为GridLayout需要组件返回它的长度。超连接组件--和其它象Label一样的SWT组件,可以通过你传递它数值来决定长和宽,但是GridLayout不会向组件传递数值参数。
我们需要的是一个象HTML表格一样的layout.我们希望内容去试图配合提供的客户空间,并且一行行地叠加.Eclipse Forms提供了一个这样的layout叫TableWrapLayout.GridLayout和TableWrapLayout之间有许多共同点.都是用表格来组织parent的children.都有layout data来告诉layout如何对待每个组件.都能够在需要占据所有空间时等接受组件的提示.
但是,它们在布局时完全不同.TableWrapLayout从列开始.它计算每列的最小的,合适的,最大的宽度并用这个信息来占据空间.它也试图尽可能地公平地在各列间分隔,这样有些组件就没有多余的空间.
让我们使用TableWrapLayout来重新更改例子(更改处高亮显示):
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createForm(parent);
form.setText("Hello, Eclipse Forms");
TableWrapLayout layout = new TableWrapLayout();
form.getBody().setLayout(layout);
Hyperlink link = toolkit.createHyperlink(form.getBody(),"Click here.", SWT.WRAP);
link.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
System.out.println("Link activated!");
}
});
link.setText("This is an example of a form that is much longer and will need to wrap.");
layout.numColumns = 2;
TableWrapData td = new TableWrapData();
td.colspan = 2;
link.setLayoutData(td);
Label label = toolkit.createLabel(form.getBody(), "Text field label:");
Text text = toolkit.createText(form.getBody(), "");
td = new TableWrapData(TableWrapData.FILL_GRAB);
text.setLayoutData(td);
Button button = toolkit.createButton(form.getBody(), "A checkbox in a form", SWT.CHECK);
td = new TableWrapData();
td.colspan = 2;
button.setLayoutData(td);
}我们用了GridData相同的概念.一些变量拥有不同的名字(举个例子,colspan和rowspan,align和valign取自HTML TABLE的属性),但是你可以做相同的事--创建一个超链接和按钮占据两列的格子.因为空白(margins)和GridLayout是相同的,结果会看起来一样,除了超链接现在会包裹起来:文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




