象其它的layout类一样,FormLayout用的data类是:FormData。FormData用另外一个类来控制窗口小部件的大小和位置: FormAttachment.一个FormData最多用4个FormAttachment,它们分别对应这个小部件的4个面:顶部,底部,左边和右边。FormAttachment定义了小部件在parent composite或是这个composite里其它部件的位置。
FormAttachment计算位置和大小的方法:
y=ax+b
在这个等式中,在数学上y代表的是纵坐标,x是横坐标值.a是斜率,b是偏移量.按照FormAttachment的规则,y是高,x是宽度,a是一个相对其它部件的百分率,b是偏移量.FormAttachment实例中的每个数据成员分别代表这些值.
以下(表一)是FormAttachment的数据成员表和相应的意思:
表一:FormAttachment数据成员
Attribute
Description
int alignment |
指定是以另外一个部件的哪一边为基准的.可能的值是:SWT.TOP, SWT.CENTER, 和 SWT.BOTTOM.默认是以最近的一边为基准. |
Control control |
指定FormAttachment是以哪个部件为参照物. |
int denominator |
指定分母.默认值为100 |
int numerator |
指定分子 |
int offset |
指定离composite边界或部件的边界的偏移量.单位是象素. |
FormAttachment 拥有5个构造函数,没有一个是空的,它们如下表(表二)所示:
表二:FormAttachment的构造函数:
Constructor
Description
FormAttachment(Control control) |
以另外的某个部件为参照物. |
FormAttachment(Control control, int offset) |
以另外的某个部件为参照物,加上偏移量. |
FormAttachment(Control control, int offset, int alignment) |
以另外的某个部件为参照物,加上偏移量和alignment |
FormAttachment(int numerator) |
设定分子.分母为100,没有偏移量 |
FormAttachment(int numerator, int offset) |
指定分子和偏移量和100的分母 |
FormAttachment(int numerator, int denominator, int offset) |
特定的分子,分母,偏移量 |
FormData最多包含4个FormAttachment实例,每个对应了与它联系的部件的一边.另外,FormData也可以指定宽和高.表四列出了FormData的数据成员:
Attribute
Description
FormAttachment bottom |
The FormAttachment corresponding to the bottom side of the control.
这个FormAttachment用来指定部件的底部位置
|
int height |
这个部件的高度.单位为象素. |
FormAttachment left |
这个FormAttachment用来指定部件的左部位置 |
FormAttachment right |
这个FormAttachment用来指定部件的右部位置 |
FormAttachment top |
这个FormAttachment用来指定部件的顶部位置 |
int width |
这个部件的宽度.单位为象素. |
当你生成一个FormData对象,你可以自己传给它宽和高的值.如果你没有指定FormAttachment对象,部件会自动以parent composite的上边界和左边为起始边界.如果你这样定义了多个部件,它们会都在composite的左上角.
FormLayout有两个数据成员 marginHeight和marginWidth,用来以象素为单位来指定大小.它们用来指定包围composite里所有内容的空白. marginHeight对应的是顶部和底部的空白大小,marginWidth对应的是左边和右边的空白大小.空白的默认值是0.
以上的话如果你看得不大懂也没有关系,我们按照具体的例子来看就知道了.
用到FormLayout的最简单的例子就是一个窗口里一个按钮,并且不用FormData.
//FormLayoutSimple.java
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.SWT;
public class FormLayoutSimple {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FormLayout());
new Button(shell, SWT.PUSH).setText("Button");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
运行后是这个样子:
你可以通过设定marginHeight和marginWidth值改变周围的空白.
代码象这样:
FormLayout layout = new FormLayout();
layout.marginHeight = 5;
layout.marginWidth = 10;
shell.setLayout(layout);
结果象这样:
但是,只有用FormData和FormAttachment你才能做更多的事.
试着使用FormData,但是不用FormAttachments,FormData有公共成员属性height和width用来改变按钮的大小.
分享到:
相关推荐
SWT教程之FormLayout介绍
SWT(JFace)体验之FormLayout布局示例代码。
org.eclipse.swt.SWT.class org.eclipse.swt.SWTError.class org.eclipse.swt.SWTException.class org.eclipse.swt.accessibility.ACC.class org.eclipse.swt.accessibility.Accessible.class org.eclipse.swt....
SWT中常用的布局管理器有如下一些。 ●FillLayout:子组件将以相同的大小填充到父组件中。 ● RowLayout:子组件将在父组件上一行或几行显示(设置相应的属性值,子组件会自动换行)。 ●GridLayout:网格式布局,子...
第10章 SWT中的拖放支持 211 10.1 可拖放的树 211 10.2 拖放原理概述 214 10.3 拖放源(DragSource) 215 10.3.1 创建拖放源对象 215 10.3.2 定义拖放源数据传输类型 215 10.3.3 处理拖放源事件 216 ...
本节将介绍SWT中布局管理器,布局管理器是最复杂的,也是最重要的。布局管理器有五种,分别是FillLayout(充满式布局)、RowLayout(行列式布局)、GridLayout(网格式布局)、FormLayout(表格式布局)和...
AppWindow,Menus,JFace_Dialogs,FormLayout,CustomComposites等一些常用组件的详细介绍!
第二章 SWT/jFace简介 36 一、SWT/jFace概述 36 二、SWT使用入门 38 三、插件环境下的Dialog 41 2.3.1 Dialog概述与典型结构 41 2.3.1 与主调程序交互 51 四、小结 53 第三章 TitleAreaDialog与布局 54 一、...
17.4 SWT中的布局管理器 302 17.4.1 RowLayout布局管理器 302 17.4.2 FillLayout布局管理器 304 17.4.3 GridLayout布局管理器 305 17.4.4 FormLayout布局管理器 308 17.5 SWT中的事件模型 309 17.6 本章小结 313 第...