Eclipse界面編寫實例(2)--理解佈局2

第二節 標準佈局

2.1. FillLayout

FillLayout是最簡單的佈局類,它把組件擺放在一行或者一列,並強制組件大小一致。一般的,組件的高度與最高組件一致,寬度與最寬組件一致。FillLayout不折行,不能設置邊界距離和間距。可以使用它佈局任務欄或工具欄,或者在Group中的一組選擇框。當容器只有一個子組件時也可以使用它。例如如果一個Shell只有一個Group子組件,FillLayout將使Group完全充滿Shell

       以下是相關代碼。首先創建了一個FillLayout,然後設置它的type域值爲SWT.VERTICAL,再把它設置到容器上(一個Shell)。示例的Shell有三個按鈕,B1, B2, Button 3。注意在FillLayout中,所有的子組件尺寸都相同,並且充滿了全部可用的空間:

   FillLayout fillLayout = new FillLayout();

   fillLayout.type = SWT.VERTICAL;

   shell.setLayout(fillLayout);

   new Button(shell, SWT.PUSH).setText("B1");

   new Button(shell, SWT.PUSH).setText("Wide Button 2");

   new Button(shell, SWT.PUSH).setText("Button 3");

       下圖顯示了水平和垂直佈局時,以及在初始狀態和調整大小之後,FillLayout的不同表現:

 

初始狀態

調整大小後

fillLayout.type = SWT.HORIZONTAL

(default)

fillLayout.type = SWT.VERTICAL

2.2 RowLayout

RowLayoutFillLayout更常用,它可以提供折行顯示,以及可設置的邊界距離和間距。它有幾個可設置的域。另外可以對每個組件通過setLayoutData方法設置RowData,來設置它們的大小。

2.2.1 RowLayout的可設置域

type *2.0新添加*

type域控制RowLayout是水平還是垂直佈局組件。默認爲水平佈局。

wrap

       wrap域控制RowLayout在當前行沒有足夠空間時是否折行顯示組件。默認折行顯示。

pack

       pack域爲true時,組件使用他們的原始尺寸,並且排列時儘量遠離左邊(and they will be aligned as far to the left as possible?)。如果pack域爲false,組件將填充可用的空間,跟FillLayout類似。默認爲true

justify

justify域爲true時,組件將在可用的空間內從左到右伸展。如果容器變大了,那麼多餘的空間被平均分配到組件上。如果packjustify同時設爲true,組件將保持它們的原始大小,多餘的空間被平均分配到組件之間的空隙上。默認的false

MarginLeft, MarginTop, MarginRight, MarginBottom 以及 Spacing

       這些域控制組件之間距離(spacing,均以像素記),以及組件與容器之間的邊距。默認的,RowLayout保留了3個像素的邊距和間距。下圖示意了邊距和間距:

2.2.2 RowLayout 示例

       下面的代碼創建了一個RowLayout,並設置各個域爲非默認值,然後把它設置到一個Shell

   RowLayout rowLayout = new RowLayout();

   rowLayout.wrap = false;

   rowLayout.pack = false;

   rowLayout.justify = true;

   rowLayout.type = SWT.VERTICAL;

   rowLayout.marginLeft = 5;

   rowLayout.marginTop = 5;

   rowLayout.marginRight = 5;

   rowLayout.marginBottom = 5;

   rowLayout.spacing = 0;

   shell.setLayout(rowLayout);

如果使用默認設置,只需要一行代碼即可:

   shell.setLayout(new RowLayout());

下圖顯示了設置不同域值的結果:

 

初始狀態
調整尺寸後

wrap = true

pack = true

justify = false

type = SWT.HORIZONTAL

(默認)

wrap = false

(沒有足夠空間時裁邊)

pack = false

(所有組件尺寸一致)

justify = true

(組件根據可用空間進行伸展)

type = SWT.VERTICAL

(組件垂直按列排列)

2.2.3 在RowLayout 上配合使用RowData

       每個由RowLayout控制的組件可以通過RowData來設置其的原始的尺寸。以下代碼演示了使用RowData改變一個Shell裏的按鈕的原始尺寸:

import org.eclipse.swt.*;

import org.eclipse.swt.widgets.*;

import org.eclipse.swt.layout.*;

public class RowDataExample {

   public static void main(String[] args) {

       Display display = new Display();

       Shell shell = new Shell(display);

       shell.setLayout(new RowLayout());

       Button button1 = new Button(shell, SWT.PUSH);

       button1.setText("Button 1");

       button1.setLayoutData(new RowData(50, 40));

       Button button2 = new Button(shell, SWT.PUSH);

       button2.setText("Button 2");

       button2.setLayoutData(new RowData(50, 30));

       Button button3 = new Button(shell, SWT.PUSH);

       button3.setText("Button 3");

       button3.setLayoutData(new RowData(50, 20));

       shell.pack();

       shell.open();

       while (!shell.isDisposed()) {

          if (!display.readAndDispatch()) display.sleep();

       }

   }

}

以下是運行結果:

 

(待續)

發佈了32 篇原創文章 · 獲贊 5 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章