posts - 89,  comments - 98,  trackbacks - 0

          SWT當(dāng)中的GridLayout是一個(gè)非常靈活的控件,但是在使用起來(lái)需要在控制上下一番功夫.

          大家都知道,JAVA在寫編寫窗口程序的時(shí)候,物件的添加,放置 操作起來(lái)要比.net費(fèi)勁的多,但是如果用好了相關(guān)org.eclipse.layout.*包當(dāng)中的相關(guān)類,也會(huì)寫出十分漂亮的界面程序.

          下面大家先看一個(gè)程序:

          源碼如下:

          package com.layout;

          import org.eclipse.swt.widgets.*;
          import org.eclipse.swt.layout.*;
          import org.eclipse.swt.SWT;

          public class CopyOfGridLayoutExc {
          ?
          ?public static void main(String[] args) {

          ??Display display = new Display();

          ???? Shell shell = new Shell(display);

          ???? shell.setText("Find (GridLayout)");

          ???? Label label = new Label(shell, SWT.NONE);

          ???? label.setText("Find what:");

          ???? Text text = new Text(shell, SWT.BORDER);

          ???? Button findButton = new Button(shell, SWT.PUSH);

          ???? findButton.setText("Find Next");

          ???? Group group = new Group(shell, SWT.NONE);

          ???? group.setLayout(new RowLayout());

          ???? Button upButton = new Button(group, SWT.RADIO);

          ???? upButton.setText("Up");

          ???? Button downButton = new Button(group, SWT.RADIO);

          ???? downButton.setText("Down");

          ???? downButton.setSelection(true);

          ???? group.setText("Direction");

          ???? Button cancelButton = new Button(shell, SWT.PUSH);

          ???? cancelButton.setText("Cancel");

          ?

          ???? /* Use a GridLayout to position the controls */

          ???? Monitor monitor = shell.getMonitor();

          ???? int width = monitor.getClientArea().width / 10;

          ???? GridLayout layout = new GridLayout(4, false);

          ???? layout.marginWidth = layout.marginHeight = 14;//layout leave's the window's space

          ???? shell.setLayout(layout);

          ???? GridData labelData =

          ???????? new GridData(SWT.FILL, SWT.CENTER, false, false);

          ???? label.setLayoutData(labelData);

          ???? GridData textData =

          ???????? new GridData(SWT.FILL,SWT.CENTER,true,false,2,1);

          ???? textData.widthHint = width;

          ???? text.setLayoutData(textData);

          ???? GridData findData =

          ???????? new GridData(SWT.FILL, SWT.CENTER, false, false);

          ???? findButton.setLayoutData(findData);

          ???? GridData groupData =

          ???????? new GridData(SWT.RIGHT,SWT.TOP,false,false,3,1);

          ???? group.setLayoutData(groupData);

          ???? GridData cancelData =

          ???????? new GridData(SWT.FILL, SWT.TOP, false, false);

          ???? cancelButton.setLayoutData(cancelData);

          ?

          ???? shell.pack();

          ???? shell.open();

          ???? while (!shell.isDisposed()) {

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

          ???? }

          ???? display.dispose();

          ?}

          }



          這其中我們?cè)谑褂玫臅r(shí)候應(yīng)該要注意以下幾點(diǎn):

          1.要選擇自己適合 的Layout類型.GridLayout適合于多種情況,它大部分情況是使用在較為復(fù)雜的界面編程當(dāng)中,因?yàn)閺?fù)雜的界面會(huì)有相當(dāng)多的控件.



          2.GridData的使用將是一個(gè)控制界面顯示的主要類.通過(guò)使用GridData我們可以很好的控制界面.

          ?? 其中GridData的構(gòu)造函數(shù)比較多,但是相關(guān)的使用我們都應(yīng)該熟悉,特別是上面源程序當(dāng)中使用的那個(gè)構(gòu)造函數(shù),在使用起來(lái)更容易控制GridLayout的布局.通過(guò)horizantalSpan,VerticalSpan來(lái)控制控件所占用的單元格,這樣就會(huì)控制其它控制是否在一列當(dāng)中顯示還是在幾列當(dāng)中顯示.前提是通過(guò)GridLayout.numColumns來(lái)設(shè)置列數(shù).

          3.如果不設(shè)置GridData那么相關(guān)的控件都會(huì)按照相關(guān)的建立順序加入到GridLayout當(dāng)中.GridData不能控制控件的顯示順序,而相關(guān)順序是對(duì)象的建立順序來(lái)控制的.這一點(diǎn)不要與GridData混淆了.

          希望寫這篇文章對(duì)大家學(xué)習(xí)SWT有用.


          posted on 2006-09-12 10:50 水煮三國(guó) 閱讀(2489) 評(píng)論(1)  編輯  收藏 所屬分類: J2SE

          FeedBack:
          # re: SWT:GridLayout 的使用需要注意的事項(xiàng)[未登錄](méi)
          2008-05-26 09:20 | javaboy
          呵呵 學(xué)習(xí)了 在我主頁(yè)上轉(zhuǎn)載了  回復(fù)  更多評(píng)論
            
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(4)

          隨筆分類(85)

          隨筆檔案(89)

          文章分類(14)

          文章檔案(42)

          收藏夾(37)

          java

          oracle

          Sybase

          搜索

          •  

          積分與排名

          • 積分 - 211651
          • 排名 - 266

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 右玉县| 天全县| 栖霞市| 瑞昌市| 佛冈县| 诸暨市| 辽阳县| 射洪县| 浦城县| 封丘县| 社会| 民县| 防城港市| 宣化县| 阜新市| 仁化县| 汝城县| 子洲县| 玛多县| 永兴县| 卢氏县| 吉林市| 乡宁县| 台东市| 芒康县| 彭州市| 靖安县| 壶关县| 清镇市| 临猗县| 乌苏市| 滁州市| 游戏| 虹口区| 张家界市| 页游| 安达市| 嘉定区| 齐河县| 碌曲县| 武川县|