SWT RowLayout 行佈局

RowLayout
RowLayoutFillLayout相似,它們放置全部的組件在單行或單列.但是RowLayout沒有強制全部的組件相同的大小.它能當組件在行或列上超出父組件空間時自動換行.
RowLayout使用RowData來決定組件的初始化大小(寬和高).你可以傳遞RowData對象給組件的setLayoutData()方法.佈局管理器(RowLayout)通過組件的RowData來決定組件的大小和位置.
注意: SWT組件中,有一個方法名爲setData()名字有點像setLayoutDate().如果你設置了控件的相關屬性,卻沒有達到你想要的效果,請確認你使用的是setLayoutData()方法.

RowData類有兩個公共的數據成員:
public int height
public int width

你可以設置這些值在創建了
RowData對象後,例如下面的代碼建立一個Button和設置它的大小爲100像數寬和50像數高:
Button button = new Button(shell, SWT.PUSH);
RowData rowData = new RowData();
rowData.height = 50;
rowData.width = 100;
button.setLayoutData(rowData); 

RowData提供兩個構造函數來指定高和寬.
構造函數 描述
public RowData(int width, int height) 單獨的設置寬和高
public RowData(Point point) new Point(100, 50),通過Point設置寬和高

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有兩個構造函數,一個爲空,一個爲帶單參數type.
下面是一個簡單的RowLayout佈局程序運行的示例:
o_RowLayout-1.jpg
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();
    }


}

RowLayoutRowData一起使用,可以在相當大程度上改變程序外觀,下面是一個例子:
r_RowLayout-2.jpg
Example:
package chapter4; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowData; 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(SWT.VERTICAL);         rowLayout.marginLeft = 10;                  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");                  Button seven = new Button(shell, SWT.PUSH);         RowData rowDate = new RowData(8080);         seven.setLayoutData(rowDate);         seven.setText("seven");                  shell.open();                  while (!shell.isDisposed()) {             if (!display.readAndDispatch()) {                 display.sleep();             }         }         display.dispose();     } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章