FillLayout是布局类中最简单的,它把所有的控件放在一行或一列,并使它们有相同的大小。它有一个公有(public)的属性和类型,决定控件是放在一行还是放在一列。可以把类型传递给构造函数,也可以在构造函数后设置类型。
构造函数:
1、public FillLayout() Constructs a FillLayout and sets type to SWT.HORIZONTAL.
2、public FillLayout(int type) Constructs a FillLayout and sets type to the passed type.
FillLayout不检查type的合法性,所以可以传递任意的int值。尽管FillLayout的默认设置是SWT.HORIZONTAL,但如果传递一个既非SWT.HORIZONTAL又非SWT.VERTICAL的值,它将使用SWT.VERTICAL。
布局类FormLayout是一个依据附件的来设置控制每个控件的位置,对应与每一个FormData都有4个设置,bottom、left、right、top设置控件的位置,如:pingpongData.bottom = new FormAttachment(basketball,0,SWT.BOTTOM); FormAttachment的构造函数有多个:
Attribute
int alignment
指定是以另外一个部件的哪一边为基准的.可能的值是:SWT.TOP , SWT.CENTER , 和 SWT.BOTTOM.默认是以最近的一边为基准.
Control control
指定 FormAttachment是以哪个部件为参照物.
int denominator
指定分母.默认值为100
int numerator
指定分子
int offset
指定离 composite边界或部件的边界的偏移量.单位是象素.
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)
特定的分子,分母,偏移 量
下面结合FillLayout和FormLayout结合做一个Group应用,并且给篮球选择框设置了一个监听器。
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("create Group");
FillLayout fill = new FillLayout(SWT.HORIZONTAL);
fill.marginHeight =5;
fill.marginWidth = 5;
fill.spacing = 10;
shell.setLayout(fill);
Group group = new Group(shell, SWT.NONE);
group.setText("业余爱好:");
group.setLayout(new FormLayout());
Label sport = new Label(group, SWT.LEFT);
FormData sportLabel = new FormData();
sportLabel.top = new FormAttachment(0,10);
sportLabel.left = new FormAttachment(0,10);
sport.setLayoutData(sportLabel);
sport.setText("运动:");
final Button basketball = new Button(group, SWT.CHECK);
FormData basketballData = new FormData();
basketballData.bottom = new FormAttachment(sport,0,SWT.BOTTOM);
basketballData.left = new FormAttachment(sport,16);
basketball.setLayoutData(basketballData);
basketball.setText("篮球");
basketball.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
if(basketball.getSelection())
System.out.println("我选择了篮球的爱好");
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
Button pingpongBut = new Button(group, SWT.CHECK);
FormData pingpongData = new FormData();
pingpongData.bottom = new FormAttachment(basketball,0,SWT.BOTTOM);
pingpongData.left = new FormAttachment(basketball,16);
pingpongBut.setLayoutData(pingpongData);
pingpongBut.setText("兵乓球");
Button footballBut = new Button(group, SWT.CHECK);
FormData footballData = new FormData();
footballData.bottom = new FormAttachment(pingpongBut, 0,SWT.BOTTOM);
footballData.left = new FormAttachment(pingpongBut, 16,SWT.RIGHT);
footballBut.setLayoutData(footballData);
footballBut.setText("足球");
Label separator = new Label(group, SWT.SEPARATOR | SWT.HORIZONTAL);
FormData separatorData = new FormData();
separatorData.top = new FormAttachment(sport, 6);
separatorData.bottom = new FormAttachment(sport, 8, SWT.BOTTOM);
separatorData.right = new FormAttachment(100, -10);
separatorData.left = new FormAttachment(sport, 0, SWT.LEFT);
separator.setLayoutData(separatorData);
Label recreation = new Label(group, SWT.NONE);
FormData recreationData = new FormData();
recreationData.left = new FormAttachment(sport, 0, SWT.LEFT);
recreationData.top = new FormAttachment(separator, 15);
recreation.setLayoutData(recreationData);
recreation.setText("娱乐:");
Button movieBut = new Button(group, SWT.CHECK);
FormData movieData = new FormData();
movieData.bottom = new FormAttachment(recreation,0,SWT.BOTTOM);
movieData.left = new FormAttachment(basketball, 0, SWT.LEFT);
movieBut.setLayoutData(movieData);
movieBut.setText("看电影");
Button gameBut = new Button(group, SWT.CHECK);
FormData ganmeData = new FormData();
ganmeData.bottom = new FormAttachment(recreation,0,SWT.BOTTOM);
ganmeData.left = new FormAttachment(pingpongBut, 0, SWT.LEFT);
gameBut.setLayoutData(ganmeData);
gameBut.setText("玩网游");
Button musicBut = new Button(group, SWT.CHECK);
FormData musicData = new FormData();
musicData.bottom = new FormAttachment(recreation,0,SWT.BOTTOM);
musicData.left = new FormAttachment(footballBut, 0, SWT.LEFT);
musicBut.setLayoutData(musicData);
musicBut.setText("听音乐");
shell.pack();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();