关于GridLayout的使用

关于GridLayout的使用:
1.       首先为容器(Composite,Group…)设置GridLayout:
              //GridLayout两个最重要的属性都可以在构造函数里设置,2代表列数;true代表是否各        //列均宽
              GridLayout gl = new GridLayout(2,true);
              composite.setLayout(gl);
2.       接下来就可以按行逐列添加控件进去
              Label label1 = new Label(composite,SWT.NONE);
              Text text1 = new Text(composite,SWT.BORDER);
              Label label2 = new Label(composite,SWT.NONE);
              Text text2 = new Text(composite,SWT.BORDER);
              这样就完成了整体的基本布局。
3.       最后,要美化和具体控制每个控件,就需要为每个控件设置GridData了
a)         GridData是专门用来和GridLayout配合指定每个控件的具体行为
b)        GridData看起来比较复杂,设置参数的方式很多。但是如果看一下GridData的源码就会发现,实质上,我们需要关注并设定的只有horizontalSpan,verticalSpan,grabExcessHorizontalSpace,grabExcessVerticalSpace,verticalAlignment,horizontalAlignment,widthHint,heightHint;这些从字面就可以看出他们的意思,下面分别介绍
                         i.              horizontalSpan和verticalSpan,这两个用来指定当前控件占据的水平和垂直格数,默认值为1。要想设置这两个参数,必须声明一个GridData变量:
       GridData gd = new GridData();
       gd.horizontalSpan = 2;
       label2.setLayoutdata(gd);
       这样这个label就占据两列了.
                       ii.              grabExcessHorizontalSpace和grabExcessVerticalSpace,这两个参数用来指定控件所在单元格是否填充的水平剩余空间和竖直剩余空间,默认值为false。这两个参数可以在构造函数中设置:
              label2.setLayoutdata(new GridData(GridData.GRAB_HORIZONTAL));
                      iii.              verticalAlignment和horizontalAlignment用来指定控件的对齐方式。有四个值:BEGINNING,CENTER,END和FILL。值得一提的是FILL,用来指定控件是否填满单元格的剩余空间。这两个参数也可以在构造函数里指定
       label2.setLayoutdata(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
              下面这种设置方式效果也是一样的
       GridData gd = new GridData();
       gd.horizontalAlignment= true;
       label2.setLayoutdata(gd);
                     iv.              widthHint和heightHint用来指定控件所在单元格的最小宽度和最小高度,默认值是SWT.DEFAULT,也就是不指定。这两个必须通过一个有名的GridData来设置:
       GridData gd = new GridData();
       gd.widthHint= 100;
       label2.setLayoutdata(gd);
                       v.              GridData提供了很多常量,通过他们的名字再结合上面的说明,很容易就知道他们究竟是设置什么的:
                     // Alignment constants.
       public static final int BEGINNING = 1;
       public static final int CENTER = 2;
       public static final int END = 3;
       public static final int FILL = 4;

       // Style constants
       public static final int VERTICAL_ALIGN_BEGINNING =  1 << 1;
       public static final int VERTICAL_ALIGN_CENTER = 1 << 2;
       public static final int VERTICAL_ALIGN_END = 1 << 3;
       public static final int VERTICAL_ALIGN_FILL = 1 << 4;
       public static final int HORIZONTAL_ALIGN_BEGINNING =  1 << 5;
       public static final int HORIZONTAL_ALIGN_CENTER = 1 << 6;
       public static final int HORIZONTAL_ALIGN_END = 1 << 7;
       public static final int HORIZONTAL_ALIGN_FILL = 1 << 8;
       public static final int GRAB_HORIZONTAL = 1 << 9;
       public static final int GRAB_VERTICAL = 1 << 10;
     
       // Style convenience constants
       /**
        * FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL
        */  
       public static final int FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL;
       /**
        * FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL
        */  
       public static final int FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL;
       /**
        * FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL
        */  
       public static final int FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL;

你可能感兴趣的:(SWT)