2.2.2 RowLayout 示例
下面的代码创建了一个RowLayout,并设置各个域为非默认值,然后把它设置到一个Shell:
RowLayout rowLayout = new RowLayout();
rowLayout.wrap = false;
rowLayout.pack = false;
rowLayout.justify = true;
rowLayout.type = SWT.VERTICAL;
rowLayout.marginLeft = 5;
rowLayout.marginTop = 5;
rowLayout.marginRight = 5;
rowLayout.marginBottom = 5;
rowLayout.spacing = 0;
shell.setLayout(rowLayout);
如果使用默认设置,只需要一行代码即可:
shell.setLayout(new RowLayout());
下图显示了设置不同域值的结果:
|
初始状态 |
调整尺寸后 |
wrap = true
pack = true
justify = false
type = SWT.HORIZONTAL
(默认) |

|

|
wrap = false
(没有足够空间时裁边) |

|

|
pack = false
(所有组件尺寸一致) |

|

|
justify = true
(组件根据可用空间进行伸展) |

|

|
type = SWT.VERTICAL
(组件垂直按列排列) |

|

|
2.2.3 在RowLayout 上配合使用RowData
每个由RowLayout控制的组件可以通过RowData来设置其的原始的尺寸。以下代码演示了使用RowData改变一个Shell里的按钮的原始尺寸:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
public class RowDataExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Button 1");
button1.setLayoutData(new RowData(50, 40));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Button 2");
button2.setLayoutData(new RowData(50, 30));
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("Button 3");
button3.setLayoutData(new RowData(50, 20));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}
}
以下是运行结果:

(待续)