通过Composite.setLayout()方法设置布局
org.eclipse.swt.widgets.Composite#setLayout(null)
setLayout(null)
通过Control.setBounds
方法设置组件位置、大小
org.eclipse.swt.widgets.Control#setBounds (int x, int y, int width, int height)
x:水平距离
y:垂直距离
(0,0)是窗体的左上角。
案例:
btnNewButton.setBounds(0, 0, 50, 50);//按钮显示在窗体的左上角宽、度高多都为50
setLayout(new FormLayout());
org.eclipse.swt.widgets.Control#setLayoutData
设置布局数据org.eclipse.swt.layout.FormData
创建布局数据Button btnNewButton = new Button(this, SWT.NONE);
FormData fd_btnNewButton = new FormData();
fd_btnNewButton.top = new FormAttachment(0, 10);
fd_btnNewButton.left = new FormAttachment(0, 10);
btnNewButton.setLayoutData(fd_btnNewButton);
btnNewButton.setText("New Button");
Button btnNewButton_1 = new Button(this, SWT.NONE);
FormData fd_btnNewButton_1 = new FormData();
fd_btnNewButton_1.top = new FormAttachment(btnNewButton, 0, SWT.TOP);//相对btnNewButton
fd_btnNewButton_1.left = new FormAttachment(btnNewButton, 6);
btnNewButton_1.setLayoutData(fd_btnNewButton_1);
btnNewButton_1.setText("New Button");
setLayout(new FillLayout(SWT.HORIZONTAL));//水平填充
setLayout(new FillLayout(SWT.VERTICAL));//垂直填充
setLayout(new GridLayout(2, false));
构造方法
public GridLayout (int numColumns, boolean makeColumnsEqualWidth)
org.eclipse.swt.widgets.Control#setLayoutData
设置组件布局数据。org.eclipse.swt.layout.GridData
设置网格数据。可以设置水平、垂直对齐类型(左对齐、居中对齐、右对齐、填充对齐),水平、垂直跨越单元格个数。GridData 构造方法
public GridData (int horizontalAlignment, int verticalAlignment, boolean grabExcessHorizontalSpace, boolean grabExcessVerticalSpace, int horizontalSpan, int verticalSpan)
setLayout(new RowLayout(SWT.VERTICAL));//水平行
setLayout(new RowLayout(SWT.HORIZONTAL));//垂直行
添加组件后会水平或垂直排列。
setLayout(new StackLayout());
添加组件按后进或先进先 填充整个窗体。
可以通过org.eclipse.swt.widgets.Composite
进行组合布局。