布局
1、Layout类
定义一个
Layout
对象。然后设置相应的容器类。则相应容器类中的控件都按这种布局排列。
FillLayout layout1=new FillLayout(); //
this.setLayout(layout1); //this
为当前容器
(Composite)
2、FillLayout
其特点是充满容器的整个客户区。如下面代码将产生如下效果:
Text text1=new Text(this,SWT.SINGLE | SWT.BORDER);
text1.setText("
测试
");
Button pbut1=new Button(this,SWT.PUSH);
pbut1.setText("Push1");
Button pbut2=new Button(this,SWT.PUSH);
pbut2.setText("Push2");
3、RowLayout
将控件按先后顺序,一行行排列,从左到右。满一行后另起一行。如:
Text text1=new Text(this,SWT.SINGLE | SWT.BORDER);
text1.setText("
测试
");
int i;
for(i=0;i<6;i++) {
Button pbut=new Button(this,SWT.PUSH);
pbut.setText("Push"+ i);
}
默认情况,
RowLayout
类会调用每个控件的
ComputeSize
方法,这样求得每个控件最小化的尺寸。如上图中,文本框的大小恰好包含里面的文字。
若想按用户的意愿进行定制,可使用
RowData
方法。
如上面程序改为:
RowData rowdata=new RowData(70,20);
this.setLayoutData(rowdata);
Text text1=new Text(this,SWT.SINGLE | SWT.BORDER);
text1.setText("
测试
");
int i;
for(i=0;i<6;i++) {
Button pbut=new Button(this,SWT.PUSH);
pbut.setLayoutData(rowdata);
pbut.setText("Push"+ i);
}
4、GridLayout
GridLayout(int numColumns, boolean makeColumnsEqualWidth)
GridLayout(); //
相当于
GridLayout(1,false);
例:
GridLayout glayout=new GridLayout();
glayout.numColumns=4;
this.setLayout(glayout);
Text text1=new Text(this,SWT.SINGLE | SWT.BORDER);
text1.setText("
测试测试测试
");
int i;
for(i=0;i<6;i++) {
Button pbut=new Button(this,SWT.PUSH);
pbut.setText("Push"+ i);
}
5、FormLayout
作为最灵活的一种布局方式。该布局的特点是根据根据已有控件的相对位置来计算新控件的位置。该算法基于概念“
attachment
”。一个“
attachment
”对象定义了与其他控件的相邻关系。
和前面几种布局比较,
FormLayout
更多的优势就在于其位置和控件顺序是无关的。而前面的布局总是按控件的建立顺序按某种规则排列。
FormLayout
使用
FormData
类来描述控件的大小和位置。
public DataQueryFace(Composite parent) {
super(parent,SWT.NONE);
FormLayout layout=new FormLayout();
List list=new List(this,SWT.BORDER|SWT.V_SCROLL);
list.add("List control");
this.setLayout(layout) ;
Text sqledit=new Text(this,SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
sqledit.setText("Text control");
//Button bt=new Button(this,SWT.NONE);
tv = new TableViewer(this, SWT.MULTI|SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
Table table = tv.getTable(); //
table.setLayoutData(new GridData(GridData.FILL_BOTH));
table.setHeaderVisible(true); //
表头显示
table.setLinesVisible(true); //
表格线显示
//TableItem it;
FormData fd1=new FormData();
fd1.top=new FormAttachment(0);
fd1.left=new FormAttachment(0);
fd1.right=new FormAttachment(20);
fd1.bottom=new FormAttachment(100);
list.setLayoutData(fd1);
FormData fd2=new FormData();
fd2.top=new FormAttachment(0);
fd2.left=new FormAttachment(list);
fd2.right=new FormAttachment(100);
fd2.bottom=new FormAttachment(25);
sqledit.setLayoutData(fd2);
FormData fd3=new FormData();
fd3.top=new FormAttachment(sqledit);
fd3.left=new FormAttachment(list);
fd3.right=new FormAttachment(100);
fd3.bottom=new FormAttachment(100);
table.setLayoutData(fd3);
}
}