14.9 FillLayout布局
FillLayout是非常简单的一种布局方式,它会以同样大小对父组件中的子组件进行布局,这些子组件将以一行或一列的形式排列。
一般来说,用户可以在任务栏、工具栏中放置FillLayout布局,通过FillLayout布局对子组件进行定位,也可以当子组件只有一个组件时,通过FillLayout布局填充整个父组件的空间。
14.9.1 FillLayout的风格
FillLayout布局中,可以把子组件按水平或垂直的方式进行排列,这些风格是当创建FillLayout实类时以参数形式指定的,如表14-2所示。
表14-2 FillLayout的风格
|
初 始 状 态 |
调整父窗口大小后的状态 |
|
|
SWT.HORIZONTAL (default) |
|
|
|
SWT.VERTICAL |
|
|
14.9.2 FillLayout布局实例
FillLayout是简单而且很常用的布局,下面通过实例展示FillLayout的布局效果,代码如例程14-14所示。
例程14-14 FillLayoutSample.java
public class FillLayoutSample {
Display display = new Display();
Shell shell = new Shell(display);
public FillLayoutSample() {
//新建FillLayout布局,设置子组件与水平方式排列
FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
//指定子组件的上、下边距为多少像素
fillLayout.marginHeight = 25;
//指定子组件的左、右边距为多少像素
fillLayout.marginWidth = 25;
//指定子组件之间距离为多少像素
fillLayout.spacing = 10;
//设定父组件的布局方式
shell.setLayout(fillLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button number 2");
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new FillLayoutSample();
}
}
程序中通过marginHeight、marginWidth和spacing指定了边距和子组件的间距,程序运行效果如图14-5所示。

图14-5 FillLayout布局实例







