替代 SWT GridData 更简单有效的方法

随便看看JFace 的package ,发现了一个名字很有意思的类GridDataFactory,可以用来代替SWT中GridData的使用,而且特别方便。以下直接copy了源代码前面的说明,使用很简单,看一下就明白了。求知欲望更强烈的可以看一下它的源代码。

* // Example1:Typicalgriddataforanon-wrappinglabel
*
* // GridDataFactoryversion
* GridDataFactory.fillDefaults().applyTo(myLabel);
*
* // EquivalentSWTversion
* GridDatalabelData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
* myLabel.setLayoutData(labelData);
*
* ////////////////////////////////////////////////////////// /
* // Example2:Typicalgriddataforawrappinglabel
*
* // GridDataFactoryversion
* GridDataFactory.fillDefaults()
* .align(SWT.FILL,SWT.CENTER)
* .hint( 150 ,SWT.DEFAULT)
* .grab( true , false )
* .applyTo(wrappingLabel);
*
* // EquivalentSWTversion
* GridDatawrappingLabelData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
* wrappingLabelData.minimumWidth = 1 ;
* wrappingLabelData.widthHint = 150 ;
* wrappingLabel.setLayoutData(wrappingLabelData);
*
* //////////////////////////////////////////////////////////////
* // Example3:Typicalgriddataforascrollablecontrol(alistbox,tree,table,etc.)
*
* // GridDataFactoryversion
* GridDataFactory.fillDefaults().grab( true , true ).hint( 150 , 150 ).applyTo(listBox);
*
* // EquivalentSWTversion
* GridDatalistBoxData = new GridData(GridData.FILL_BOTH);
* listBoxData.widthHint = 150 ;
* listBoxData.heightHint = 150 ;
* listBoxData.minimumWidth = 1 ;
* listBoxData.minimumHeight = 1 ;
* listBox.setLayoutData(listBoxData);
*
* //////////////////////////////////////////////////////////// /
* // Example4:Typicalgriddataforabutton
*
* // GridDataFactoryversion
* PointpreferredSize = button.computeSize(SWT.DEFAULT,SWT.DEFAULT, false );
* Pointhint = Geometry.max(LayoutConstants.getMinButtonSize(),preferredSize);
* GridDataFactory.fillDefaults().align(SWT.FILL,SWT.CENTER).hint(hint).applyTo(button);
*
* // EquivalentSWTversion
* PointpreferredSize = button.computeSize(SWT.DEFAULT,SWT.DEFAULT, false );
* Pointhint = Geometry.max(LayoutConstants.getMinButtonSize(),preferredSize);
* GridDatabuttonData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
* buttonData.widthHint = hint.x;
* buttonData.heightHint = hint.y;
* button.setLayoutData(buttonData);

类似的还有一个GridLayoutFactory。

你可能感兴趣的:(grid)