关于SWT中的GridLayout布局方式

GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式。GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中。

GridLayout布局 

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

GridLayout的风格 

GridLayout类提供了GridLayout 布局中划分网格的信息,主要通过以下几个参数进行设置。 

NumColumns:通过“gridLayout.numColumns”属性可以设置父组件中分几列显示子组件。 

MakeColumnsEqualWidth:通过“gridLayout. makeColumnsEqualWidth”属性可以设置父组件中子组件是否有相同的列宽当MakeColumnsEqualWidth为true时表示每列的列宽相等。 
MarginLeft:表示当前组件距离父组件左边距的像素点个数。 
MarginRight:表示当前组件距离父组件右边距的像素点个数。 
MarginTop:表示当前组件距离父组件上边距的像素点个数。 
MarginBottom:表示当前组件距离父组件下边距的像素点个数。 
HorizontalSpacing:表示子组件的水平间距。 
VerticalSpacing:表示子组件的垂直间距。 

GridData的相关属性 

GridLayout布局的灵活之处在于它利用网格布局数据GridData。通过GridData可以设置子组件在网格中的填充方式、大小边距等信息,用户可以通过子组件的setLayoutData方法设置网格布局数据。 

GridData可以控制子组件在网格中的位置大小等相关显示信息。GridData可以设置如下的一些属性。 

HorizontalAlignment:表示水平对齐方式。 

VerticalAlignment:表示子组件的垂直对齐方式,值和水平方式一样。 
HorizontalIndent:表示子组件水平偏移多少像素。此属性和“horizontalAlignment = GridData.BEGINNING”属性一起使用。 

HorizontalSpan:表示组件水平占据几个网格。 

GrabExcessHorizontalSpace:表示当父组件大小改变时,子组件是否以水平方向抢占空间。 
GrabExcessVerticalSpace:表示当父组件大小改变时,子组件是否以垂直方向抢占空间。 
WidthHint:表示子组件的宽度为多少像素(前提是未设置其他相关属性)。 
HeightHint:表示子组件的高度为多少像素(前提是未设置其他相关属性)。 

另外,GridData可以通过构造函数指定相应的属性值,有兴趣的读者可以参考GridData类的构造函数。 .

14.11.1 GridLayout的风格

GridLayout类提供了GridLayout 布局中划分网格的信息,主要通过以下几个参数进行设置。

NumColumns:通过“gridLayout.numColumns”属性可以设置父组件中分几列显示子组件,如表14-4所示。

表14-4  NumColumns效果

列    数

显 示 效 果

numColumns = 1

 

numColumns = 2

 

numColumns = 3

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

GridLayout1.java

 1 import org.eclipse.swt.SWT;
 2 import org.eclipse.swt.layout.GridLayout;
 3 import org.eclipse.swt.widgets.Button;
 4 import org.eclipse.swt.widgets.Display;
 5 import org.eclipse.swt.widgets.Shell;
 6 
 7 public class GridLayout1 {
 8     public static void main(String[] args) {
 9         final Display display = Display.getDefault();
10         final Shell shell = new Shell();
11         shell.setSize(327, 253);
12         // ---------创建窗口中的其他界面组件-------------
13         // 把空间分3列。建立5个按钮,由左向右排,排满3个后换行继续
14         shell.setLayout(new GridLayout(3, false));
15         new Button(shell, SWT.NONE).setText("b1");
16         new Button(shell, SWT.NONE).setText("button2");
17         new Button(shell, SWT.NONE).setText("b3");
18         new Button(shell, SWT.NONE).setText("button4");
19         new Button(shell, SWT.NONE).setText("button5");
20         // -----------------END------------------------
21         shell.layout();
22         shell.open();
23         while (!shell.isDisposed()) {
24             if (!display.readAndDispatch())
25                 display.sleep();
26         }
27         display.dispose();
28     }
29 }

GridLayout1.java

 1 public class GridData1 {
 2     public static void main(String[] args) {
 3         final Display display = Display.getDefault();
 4         final Shell shell = new Shell();
 5         shell.setSize(327, 253);
 6         // ---------创建窗口中的其他界面组件-------------
 7         shell.setLayout(new GridLayout(2, false));
 8         new Button(shell, SWT.NONE).setText("b1");
 9         new Button(shell, SWT.NONE).setText("button2");
10 
11         // 定义一个GridData对象,让b3按钮抢占两列的空间
12         Button b3 = new Button(shell, SWT.NONE);
13         GridData gridData = new GridData();
14         // GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
15         gridData.horizontalSpan = 2;
16         b3.setLayoutData(gridData);
17         b3.setText("b3");
18 
19         new Button(shell, SWT.NONE).setText("button4");
20         new Button(shell, SWT.NONE).setText("button5");
21 
22 //        Button button6 = new Button(shell, SWT.NONE);
23 //        GridData gridData2 = new GridData(GridData.FILL_HORIZONTAL);
24 //        button6.setLayoutData(gridData2);
25 //        button6.setText("button6");
26 
27         // -----------------END------------------------
28         shell.layout();
29         shell.open();
30         while (!shell.isDisposed()) {
31             if (!display.readAndDispatch())
32                 display.sleep();
33         }
34         display.dispose();
35     }
36 }

GridData2.java

 1 public class GridData2 {
 2     public static void main(String[] args) {
 3         final Display display = Display.getDefault();
 4         final Shell shell = new Shell();
 5         shell.setSize(327, 253);
 6         // ---------创建窗口中的其他界面组件-------------
 7         shell.setLayout(new GridLayout());
 8         Button b1 = new Button(shell, SWT.NONE);
 9         GridData gridData = new GridData();
10         gridData.horizontalAlignment = GridData.BEGINNING;
11         b1.setLayoutData(gridData);
12         b1.setText("b1");
13         new Button(shell, SWT.NONE).setText("button2");
14         // -----------------END------------------------
15         shell.layout();
16         shell.open();
17         while (!shell.isDisposed()) {
18             if (!display.readAndDispatch())
19                 display.sleep();
20         }
21         display.dispose();
22     }
23 }

GridData3.java

 1 public class GridData3 {
 2     public static void main(String[] args) {
 3         final Display display = Display.getDefault();
 4         final Shell shell = new Shell();
 5         shell.setSize(327, 253);
 6         // ---------创建窗口中的其他界面组件-------------
 7         shell.setLayout(new GridLayout());
 8         Button b1 = new Button(shell, SWT.NONE);
 9         GridData gridData = new GridData();
10         gridData.grabExcessHorizontalSpace = true;
11         gridData.horizontalAlignment = GridData.FILL;
12         // 以上三句和GridData gridData = new GridData(GridData.FILL_HORIZONTAL);等效
13         b1.setLayoutData(gridData);
14         b1.setText("b1");
15         new Button(shell, SWT.NONE).setText("button2");
16         // -----------------END------------------------
17         shell.layout();
18         shell.open();
19         while (!shell.isDisposed()) {
20             if (!display.readAndDispatch())
21                 display.sleep();
22         }
23         display.dispose();
24     }
25 }

 

原文地址:https://www.cnblogs.com/DreamDrive/p/4256524.html