RowLayout | ||||||
RowLayout和FillLayout相似,它們放置全部的組件在單行或單列.但是RowLayout沒有強制全部的組件相同的大小.它能當組件在行或列上超出父組件空間時自動換行. RowLayout使用RowData來決定組件的初始化大小(寬和高).你可以傳遞RowData對象給組件的setLayoutData()方法.佈局管理器(RowLayout)通過組件的RowData來決定組件的大小和位置. |
||||||
注意: 在SWT組件中,有一個方法名爲setData()名字有點像setLayoutDate().如果你設置了控件的相關屬性,卻沒有達到你想要的效果,請確認你使用的是setLayoutData()方法. | ||||||
RowData類有兩個公共的數據成員: |
||||||
RowData提供兩個構造函數來指定高和寬.
RowLayout和FillLayout相似,也提供屬性type(SWT.HORIZONTAL或SWT.VERTICAL)來配置佈局爲行或列.RowLayout同時也提供另外幾個屬性來定製佈局風格. RowLayout的屬性 |
屬性 | 描述 |
boolean justify | 如果爲true,類似於平均分配當前行或行上的空間.默認爲false. |
int marginBottom, marginLeft, marginRight, marginTop | 分別定義底,左,右,上邊距,以像素爲單位.默認爲3像素. |
boolean pack | 如果爲true,通知控件調整爲它自已最合適的大小,默認爲true. |
int spacing | 設置鄰近控件間的間距,像素爲單位,默認爲3像素. |
int type | 佈局管理器中控件佈局的方式,默認爲SWT.HORIZONTAL按行排列,SWT.VERTICAL時按列排列. |
boolean warp | 如果爲true,當行或列的超出佈局空間大小時自動換行到下一行或列.默爲認true. |
boolean fill | 當水平填充時,子控件是否等高,當垂直填充時,子控件是否等寬.默認爲false. |
下面是一個簡單的RowLayout佈局程序運行的示例:
Example:
package chapter4;
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * @author HexUzHoNG * */ public class RowLayoutDemo { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); RowLayout rowLayout = new RowLayout(); //可以通過設置相關屬性來改變外觀 :-) rowLayout.justify = true; shell.setLayout(rowLayout); new Button(shell, SWT.PUSH).setText("one"); new Button(shell, SWT.PUSH).setText("two"); new Button(shell, SWT.PUSH).setText("three"); new Button(shell, SWT.PUSH).setText("four"); new Button(shell, SWT.PUSH).setText("five"); new Button(shell, SWT.PUSH).setText("six"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } |
RowLayout和RowData一起使用,可以在相當大程度上改變程序外觀,下面是一個例子: |