SWT有2种控制控件位置和大小的方法:绝对定位和托管定位。
绝对定位是相对于父容器的左上角来说的。它的缺点是,窗口变化时,控件的位置和大小是不变的。
托管定位:只要把控件放到容器里面即可,它们的位置和大小由布局管理器来计算和维护。当窗口发生改变是,管理器会重新计算控件的位置和大小。
package com.test.cxm.plugin01.perspective;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class HelloWorldSWT {
public static void main(String args[]){
Display display=Display.getDefault();
Shell shell=new Shell();
shell.setBounds(100, 100, 220, 180);
shell.setText("mmm");
//填充布局,部件完全填充其父部件
//每个小部件都和其它小部件的大小保存一致
//不能控制部件的间隔、页边距等属性
//默认为SWT.HORIZONTAL默认为横线排列
//shell.setLayout(new FillLayout(SWT.VERTICAL))这个为垂直排列
shell.setLayout(new FillLayout());
for(int i=0;i<8;i++){
Button bn=new Button(shell, SWT.PUSH);
bn.setText("B"+i);
}
还可以这样:
FillLayout fill=new FillLayout();
fill.type=SWT.VIRTUAL;
fill.marginHeight=10;//上下边框的距离
fill.marginWidth=30;//左右边框的距离
fill.spacing=10;//控件间的间隔
shell.setLayout(fill);
//行布局
//和填充布局类似,将小部件以一行或一列的形式放置,还可以控制许多附加属性
//子部件和父shell间的空白通过marginLeft;marginRigh;marginTop;marginBottom这几个属性设置
//部件间的距离通过spacing设置
RowLayout rlay=new RowLayout();
rlay.marginLeft=5;
rlay.marginRight=5;
rlay.marginTop=10;
rlay.marginBottom=5;
rlay.spacing=10;
rlay.wrap=false;//是否自动换行,默认为ture
rlay.pack=false;//根据控件的内容,调整其大小,默认为true
b.setLayoutData(new RowData(100,30));//还可以设置某一个控件的长和宽
shell.setLayout(rlay);
for(int i=0;i<8;i++){
Button bn=new Button(shell, SWT.PUSH);
bn.setText("B"+i);
}
//网格布局,是最常用、也是最复杂的布局管理器
//布局中得每个小部件,都可以通过GridData对象控制
GridLayout gl=new GridLayout();
gl.numColumns=2;//定义该布局管理器的列数
shell.setLayout(gl);
Label label=new Label(shell,SWT.LEFT);
label.setText("label");
GridData gd=new GridData();
gd.horizontalSpan=2;//指定小部件占据的列数,默认为一个单元格
label.setLayoutData(gd);
//注意:GridData对象不能重用
Label label2=new Label(shell,SWT.LEFT);
label2.setText("name");
Text text=new Text(shell, SWT.SINGLE|SWT.BORDER);
GridData gd2=new GridData();
//horizontalAlignment属性指定小部件的水平对齐方式
//有SWT.BEGINNING CENTER END FILL
//FILL指的是让该小部件充满该单元格
gd2.horizontalAlignment=GridData.FILL;
//定义是否填充单元格,只有这个属性为true时,上面的属性才能生效
gd2.grabExcessHorizontalSpace=true;
text.setLayoutData(gd2);
// horizontalAlignment是水平对齐方式,相应的还有verticalAligment垂直对齐方式
//horizontalIndent水平缩进,verticalIndent垂直缩进
//horizontalSpan跨列显示,verticalSpan跨行显示
/**
* 表单布局
* 它是eclipse中最为强大的布局
* 通过它可以独立控制部件的四周,顶部、底部、左侧、右侧
*
* 上下都是参考上边框,左右都是参考左边框
* 正值表示向下和向右偏移的像素点,负的表示向上和向左
* FormData是最重要的对象,它具有四个FormAtttachment对象
* 每一个对应一个边框
*
*/
shell.setLayout(new FormLayout());
Button bn=new Button(shell, SWT.PUSH);
bn.setText("Cancel");
//默认没有参数,可以带参数,表示组件新的宽度和高度
//new FormData(100,50)
FormData fm=new FormData();
fm.right=new FormAttachment(100,-5);
fm.bottom=new FormAttachment(100,-5);
bn.setLayoutData(fm);
Button bn2=new Button(shell, SWT.PUSH);
bn2.setText("ok");
FormData fm2=new FormData();
//可以有第3个参数,这个参数时可选的
//虽然指定了组件,但是以该组件的那个地方为标准偏移的
//可以取得值为:LEFT,RIGHT,CENTER,TOP,BUTTON,DEFAULT
fm2.right=new FormAttachment(bn,-5,SWT.CENTER);
fm2.bottom=new FormAttachment(100,-5);
bn2.setLayoutData(fm2);
Text tx=new Text(shell, SWT.MULTI|SWT.BORDER);
FormData tfm=new FormData();
tfm.top=new FormAttachment(0,5);
tfm.bottom=new FormAttachment(bn,-5);
tfm.left=new FormAttachment(0,5);
tfm.right=new FormAttachment(100,-5);
tx.setLayoutData(tfm);
堆栈式布局管理器,就像迭起的扑克牌,只会显示最上面的那个控件
final Composite comp1=new Composite(shell, SWT.BORDER);
final StackLayout st=new StackLayout();
comp1.setLayout(st);
final Button bt1=new Button(comp1,SWT.RADIO);
final Button bt2=new Button(comp1,SWT.CHECK);
bt1.setText("radio");
bt2.setText("check");
final Composite comp2=new Composite(shell, SWT.BORDER);
comp2.setLayout(new RowLayout());
Button bt3=new Button(comp2, SWT.NONE);
bt3.setText("Radio");
bt3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e){
st.topControl=bt1;
comp1.layout();
}
});
Button bt4=new Button(comp2, SWT.NONE);
bt4.setText("Check");
bt4.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e){
st.topControl=bt2;
comp1.layout();
}
});
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}