GridLayout布局.docx

上传人:小飞机 文档编号:3157683 上传时间:2023-03-11 格式:DOCX 页数:9 大小:39.28KB
返回 下载 相关 举报
GridLayout布局.docx_第1页
第1页 / 共9页
GridLayout布局.docx_第2页
第2页 / 共9页
GridLayout布局.docx_第3页
第3页 / 共9页
GridLayout布局.docx_第4页
第4页 / 共9页
GridLayout布局.docx_第5页
第5页 / 共9页
亲,该文档总共9页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《GridLayout布局.docx》由会员分享,可在线阅读,更多相关《GridLayout布局.docx(9页珍藏版)》请在三一办公上搜索。

1、GridLayout布局14.11 GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式。GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中。 GridLayout提供了很多的属性,可以灵活设置网格的信息。另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如“dogPhoto.setLayoutData(gridData)”,GridData可以设置每个组件当做单元格的信息。 14.11.1 GridLayout的

2、风格 GridLayout类提供了GridLayout 布局中划分网格的信息,主要通过以下几个参数进行设置。 l NumColumns:通过“gridLayout.numColumns”属性可以设置父组件中分几列显示子组件,如表14-4所示。 表14-4 NumColumns效果 列 数 显 示 效 果 numColumns = 1 numColumns = 2 numColumns = 3 l MakeColumnsEqualWidth:通过“gridLayout. makeColumnsEqualWidth”属性可以设置父组件中子组件是否有相同的列宽,当MakeColumnsEqualWi

3、dth为true时表示每列的列宽相等。 l MarginLeft: 表示当前组件距离父组件左边距的像素点个数。 l MarginRight:表示当前组件距离父组件右边距的像素点个数。 l MarginTop: 表示当前组件距离父组件上边距的像素点个数。 l MarginBottom:表示当前组件距离父组件下边距的像素点个数。 l HorizontalSpacing:表示子组件的水平间距。 l VerticalSpacing:表示子组件的垂直间距。 14.11.2 GridData的相关属性 GridLayout布局的灵活之处在于它利用网格布局数据GridData。通过GridData可以设置子

4、组件在网格中的填充方式、大小边距等信息,用户可以通过子组件的setLayoutData方法设置网格布局数据。 GridData可以控制子组件在网格中的位置大小等相关显示信息。GridData可以设置如下的一些属性。 l HorizontalAlignment:表示水平对齐方式。水平对齐方式有如下几种,如表14-5所示,其中“Button5”按钮显示了水平对齐的方式。 表14-5 组件水平对齐方式 HorizontalAlignment的值 horizontalAlignment = GridData.BEGINNING(default) 显 示 效 果 horizontalAlignment

5、= GridData.CENTER horizontalAlignment = GridData.END horizontalAlignment = GridData.FILL l VerticalAlignment:表示子组件的垂直对齐方式,值和水平方式一样。 l HorizontalIndent:表示子组件水平偏移多少像素。 此属性和“horizontalAlignment = GridData.BEGINNING”属性一起使用。下面代码设置“Button5”水平偏移4像素,如图14-7所示。 GridData gridData = new GridData; gridData.horiz

6、ontalIndent = 4; button5.setLayoutData(gridData); l HorizontalSpan:表示组件水平占据几个网格。 此属性非常有用,当要设置一个组件占据几个单元格时,需要设置HorizontalSpan属性。例如,下面代码设置“Button5”按钮水平占据两个网格,如图14-8所示。 GridData gridData = new GridData( ); gridData.horizontalAlignment = GridData.FILL; gridData.horizontalSpan = 2; button5.setLayoutData(

7、gridData); 图14-7 组件水平偏移 图14-8 水平占据网格 l VerticalSpan:表示组件垂直占据几个网格。 l GrabExcessHorizontalSpace:表示当父组件大小改变时,子组件是否以水平方向抢占空间。 l GrabExcessVerticalSpace:表示当父组件大小改变时,子组件是否以垂直方向抢占空间。 l WidthHint:表示子组件的宽度为多少像素。 l HeightHint:表示子组件的高度为多少像素。 另外,GridData可以通过构造函数指定相应的属性值,有兴趣的读者可以参考GridData类的构造函数。 14.11.3 GridLay

8、out 布局实例 为了更深入地理解GridLayout 布局,下面通过具体的实例演示如何构建一个比较复杂的布局窗口。通过本例的学习,读者可以比较好地掌握GridLayout布局,代码如例程14-16所示。 例程14-16 ComplexGridLayoutExample.java public class ComplexGridLayoutExample Static Display display; static Shell shell; static Text dogName; static Combo dogBreed; static Canvas dogPhoto; static Im

9、age dogImage; static List categories; static Text ownerName; static Text ownerPhone; public static void main(String args) display = new Display; shell = new Shell(display); shell.setText(Dog Show Entry); /新建GridLayout布局 GridLayout gridLayout = new GridLayout( ); /把子组件分成3列显示 gridLayout.numColumns = 3

10、; shell.setLayout(gridLayout); new Label(shell, SWT.NONE).setText(Dogs Name:); dogName = new Text(shell, SWT.SINGLE | SWT.BORDER); /新建水平填充的GridData GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); /GridData的组件占两列显示 gridData.horizontalSpan = 2; dogName.setLayoutData(gridData); new La

11、bel(shell, SWT.NONE).setText(Breed:); dogBreed = new Combo(shell, SWT.NONE); dogBreed.setItems(new String Collie, Pitbull, Poodle, Scottie); dogBreed.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL); Label label = new Label(shell, SWT.NONE); label.setText(Categories); label.setLayoutData(n

12、ew GridData(GridData.HORIZONTAL_ALIGN_CENTER); new Label(shell, SWT.NONE).setText(Photo:); dogPhoto = new Canvas(shell, SWT.BORDER); /gridData两端填充 gridData = new GridData(GridData.FILL_BOTH); /gridData最佳宽度和高度 gridData.widthHint = 80; gridData.heightHint = 80; /设置gridData的子组件垂直占3行 gridData.verticalSp

13、an = 3; dogPhoto.setLayoutData(gridData); /添加画布的重画事件 dogPhoto.addPaintListener(new PaintListener public void paintControl(final PaintEvent event) if (dogImage != null) event.gc.drawImage(dogImage, 0, 0); ); categories = new List(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); categories.setItems(new

14、String Best of Breed, Prettiest Female, Handsomest Male, Best Dressed, Fluffiest Ears, Most Colors, Best Performer, Loudest Bark, Best Behaved, Prettiest Eyes, Most Hair, Longest Tail, Cutest Trick); gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData. VERTICAL_ALIGN_FILL); gridData.ve

15、rticalSpan = 4; Int listHeight = categories.getItemHeight( ) * 12; Rectangle trim = puteTrim(0, 0, 0, listHeight); gridData.heightHint = trim.height; categories.setLayoutData(gridData); Button browse = new Button(shell, SWT.PUSH); browse.setText(Browse.); gridData = new GridData(GridData.HORIZONTAL_

16、ALIGN_FILL); gridData.horizontalIndent = 5; browse.setLayoutData(gridData); /添加按钮的选择事件 browse.addSelectionListener(new SelectionAdapter public void widgetSelected(SelectionEvent event) String fileName = new FileDialog(shell).open; if (fileName != null) dogImage = new Image(display, fileName); dogPho

17、to.redraw; ); Button delete = new Button(shell, SWT.PUSH); delete.setText(Delete); gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData. VERTICAL_ALIGN_BEGINNING); gridData.horizontalIndent = 5; delete.setLayoutData(gridData); delete.addSelectionListener(new SelectionAdapter public void

18、 widgetSelected(SelectionEvent event) if (dogImage != null) dogImage.dispose; dogImage = null; dogPhoto.redraw; ); Group ownerInfo = new Group(shell, SWT.NONE); ownerInfo.setText(Owner Info); gridLayout = new GridLayout; gridLayout.numColumns = 2; ownerInfo.setLayout(gridLayout); gridData = new Grid

19、Data(GridData.HORIZONTAL_ALIGN_FILL); gridData.horizontalSpan = 2; ownerInfo.setLayoutData(gridData); new Label(ownerInfo, SWT.NONE).setText(Name:); ownerName = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER); ownerName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL); new Label(ownerInfo, SWT.NONE

20、).setText(Phone:); ownerPhone = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER); ownerPhone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL); Button enter = new Button(shell, SWT.PUSH); enter.setText(Enter); gridData = new GridData(GridData.HORIZONTAL_ALIGN_END); gridData.horizontalSpan = 3; enter.

21、setLayoutData(gridData); enter.addSelectionListener(new SelectionAdapter public void widgetSelected(SelectionEvent event) System.out.println(nDog Name: + dogName.getText); System.out.println(Dog Breed: + dogBreed.getText); System.out.println(Owner Name: + ownerName.getText); System.out.println(Owner

22、 Phone: + ownerPhone.getText); System.out.println(Categories:); String cats = categories.getSelection; for (int i = 0; i cats.length; i+) System.out.println(t + catsi); ); shell.pack; shell.open; while (!shell.isDisposed) if (!display.readAndDispatch) display.sleep; if (dogImage != null) dogImage.dispose; 程序中构建了一个比较复杂的窗口,设置了GridLayout的布局信息和相关的GridData网格数据信息,程序运行效果如图14-9所示。 图14-9 GridLayout布局实例

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号