`

SWT布局--GridLayout&&GridData

    博客分类:
  • swt
 
阅读更多

GridLayout网格布局在swt中应用广泛,在做企业rcp的时候,页面上用到的大部分是该布局。GridLayout既然是网格布局,在界面上就类似网格一样,一行中分为几列,类似表格。每行的列数不一定一样,也不一定对齐,这需要我们去对GridData进行编辑。GridLayout执行layout这个方法时,也是取得控件里面的children的GridData,进行计算。

 

下面来了解一下GridLayout这个布局。

 

构造方法:

public GridLayout () {}
//numColumns为列数,makeColumnEqualsWidth为每列是否同样宽度
public GridLayout (int numColumns, boolean makeColumnsEqualWidth) {}

 GridLayout里面的属性,基本都是描述布局的边距像素,行数

	public int numColumns = 1;//默认为1列
	public boolean makeColumnsEqualWidth = false;//默认每列不相等
 	public int marginWidth = 5;//左右边距为5
 	public int marginHeight = 5;//上下边距为5
	public int marginLeft = 0;//左边距0
	public int marginTop = 0;//上边距0
	public int marginRight = 0;//右边距0
	public int marginBottom = 0;//底部边距0
 	public int horizontalSpacing = 5;//水平间隔5
 	public int verticalSpacing = 5;//上下间隔5

 

GridLayout与GridData相配合,效果才好。

/**
	 * verticalAlignment 指定控件的竖直方向的位置
	 * The default value is CENTER.
	 * Possible values are: <ul>
	 *    <li>SWT.BEGINNING (or SWT.TOP):上部</li>
	 *    <li>SWT.CENTER: 中间</li>
	 *    <li>SWT.END (or SWT.BOTTOM): 底部</li>
	 *    <li>SWT.FILL: 上下充满</li>
	 * </ul>
	 */
	public int verticalAlignment = CENTER;
	
	/**
	 * 指定水平位置
	 * Possible values are: <ul>
	 *    <li>SWT.BEGINNING (or SWT.LEFT): Position the control at the left of the cell</li>
	 *    <li>SWT.CENTER: Position the control in the horizontal center of the cell</li>
	 *    <li>SWT.END (or SWT.RIGHT): Position the control at the right of the cell</li>
	 *    <li>SWT.FILL: Resize the control to fill the cell horizontally</li>
	 * </ul>
	 */
	public int horizontalAlignment = BEGINNING;
	public int widthHint = SWT.DEFAULT;//宽度
	public int heightHint = SWT.DEFAULT;//高度
	public int horizontalIndent = 0;//水平缩进
	public int verticalIndent = 0;//竖直向下延伸
	public int horizontalSpan = 1;//所占水平方向的格数
	public int verticalSpan = 1;//所占竖直方向的格数
        //如果父控件在水平方向还有剩余空间,如果为true,则水平填满该控件
	public boolean grabExcessHorizontalSpace = false;
        //同上,在竖直方向填满
	public boolean grabExcessVerticalSpace = false;
	public int minimumWidth = 0;
	public int minimumHeight = 0;
其余的属性则是一些int值

 其实,GridData也就是描述一下对应的控件其位置的属性。不要忘了,new GridData(....)之后,对应的obj要

control.setLayoutData(Object layoutData);

 

接下来就看一个实例吧。

下面这个图是工作项目中使用到的一个GridData例子

 



 


说明:

上部容器分为了两个,左边按钮与右边时间轴缩放控件。左边的容器又分成了七个按钮,每个按钮都是相同大小,这就需要在layout里设置每个grid都是相同宽度。有个细节是查询任务与容器的左边距有一定的距离,这里也是需要设置的。右边容器要设置向右靠齐,不然则会贴在左边容器的右边。ok,贴代码。

 

//父容器设置layout为gridLayout

private void initTop(Composite comp) {
		topComp = new Composite(comp, SWT.BORDER);
		topComp.setLayout(new GridLayout(2,false));//上部面板设置为两列
		GridData topCompGridData = new GridData();
		topCompGridData.heightHint=60;//面板高度为60
		topCompGridData.horizontalAlignment=SWT.FILL;//也可用horizontalHint进行设置,水平充满
		topCompGridData.grabExcessHorizontalSpace=true;//这点要设置,不然上部面板不会充满父容器,这两个属性搭配
		topComp.setLayoutData(topCompGridData);
		
		Composite topLeft = new Composite(topComp,SWT.LEFT);//左边按钮容器
		topLeft.setLayout(new GridLayout(7,true));//7列,每列同样宽度
		GridData topRightGridData = new GridData(SWT.END,SWT.CENTER,true,false);
		
		Composite topRight = new Composite(topComp,SWT.RIGHT);
		topRight.setLayoutData(topRightGridData);
		topRight.setLayout(new GridLayout(2,false));//两列不同宽度
		
		// 查询按钮
		{
			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			gd.horizontalIndent = 10;//水平缩进10像素
			queryTaskButton = new Button(topLeft,SWT.None);
			queryTaskButton.setLayoutData(gd);
			queryTaskButton.setText("查询任务");
			
			
		}
                {
                        // 其余按钮就不显示了
                 }
}	

 

  • 大小: 11.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics