gr8vyguy@Blogjava

          SWT中模擬AWT的BorderLayout

          BorderLayout JFrame 的默認布局類,相信大家都用過,SWT沒有提供這個Java程序員非常熟悉的Layout類。我們怎們來自己定義一個呢?首先要稍微了解一下Layout的內(nèi)部實現(xiàn)原理。

          borderlayout.jpg

          Layouts 是一個容器用來對其子成員布局的一個算法,符合 Strategy Design Pattern . 當(dāng) SWT 打開一個 Composite 時,會調(diào)用 Composite 里的 layout.computeSize() 計算 Composite 的大小,然后再調(diào) layout.layout() 設(shè)置子成員的位置和大小 . 如果需要, layout 會調(diào)用子成員的 getLayoutData() 來獲得單個子成員特別的屬性。

          computeSize()
          layout() 是抽象類 Layout 的兩個抽象方法。

          要定義一個新的 layout ,也就是要定義一個 Layout 的子類, 實現(xiàn) computeSize layout. BorderLayout 來說,我們需要區(qū)分子控件是在哪個位置的 , WEST 的,還是 EAST 的,還是 CENTER 的,這個屬性通過 Control.setLayoutData() 方法保存的各個控件里。

          廢話少說了,先看源代碼

          public ? class ?BorderLayout? extends ?Layout?{
          ????
          private ?Control?north;
          ????
          private ?Control?south;
          ????
          private ?Control?east;
          ????
          private ?Control?west;
          ????
          private ?Control?center;

          ????
          protected ? void ?getControls(Composite?composite)?{
          ????????Control[]?children? = ?composite.getChildren();
          ????????
          for ?( int ?i? = ? 0 ,?n? = ?children.length;?i? < ?n;?i ++ )?{
          ????????????Control?child?
          = ?children[i];
          ????????????BorderData?borderData?
          = ?(BorderData)?child.getLayoutData();
          ????????????
          if ?(borderData? == ?BorderData.NORTH)
          ????????????????north?
          = ?child;
          ????????????
          else ? if ?(borderData? == ?BorderData.SOUTH)
          ????????????????south?
          = ?child;
          ????????????
          else ? if ?(borderData? == ?BorderData.EAST)
          ????????????????east?
          = ?child;
          ????????????
          else ? if ?(borderData? == ?BorderData.WEST)
          ????????????????west?
          = ?child;
          ????????????
          else
          ????????????????center?
          = ?child;
          ????????}
          ??? }
          }

          Control的Layout Data可以用Control.setLayoutData()方法設(shè)定, 所以getControl()方法找著各個控件的相應(yīng)位置。

          ???? protected ?Point?computeSize(Composite?composite,? int ?wHint,? int ?hHint,
          ????????????
          boolean ?flushCache)?{
          ????????getControls(composite);
          ????????
          int ?width? = ? 0 ,?height? = ? 0 ;

          ????????width? += ?west? == ? null ? ? ? 0 ?:?getSize(west,?flushCache).x;
          ????????width?
          += ?east? == ? null ? ? ? 0 ?:?getSize(east,?flushCache).x;
          ????????width?
          += ?center? == ? null ? ? ? 0 ?:?getSize(center,?flushCache).x;

          ????????
          if ?(north? != ? null )?{
          ????????????Point?pt?
          = ?getSize(north,?flushCache);
          ????????????width?
          = ?Math.max(width,?pt.x);
          ????????}
          ????????
          if ?(south? != ? null )?{
          ????????????Point?pt?
          = ?getSize(south,?flushCache);
          ????????????width?
          = ?Math.max(width,?pt.x);
          ????????}

          ??????? height? += ?north? == ? null ? ? ? 0 ?:?getSize(north,?flushCache).y;
          ????????height?
          += ?south? == ? null ? ? ? 0 ?:?getSize(south,?flushCache).y;

          ????????
          int ?heightOther? = ?center? == ? null ? ? ? 0 ?:?getSize(center,?flushCache).y;
          ????????
          if ?(west? != ? null )?{
          ????????????Point?pt?
          = ?getSize(west,?flushCache);
          ????????????heightOther?
          = ?Math.max(heightOther,?pt.y);
          ????????}
          ????????
          if ?(east? != ? null )?{
          ????????????Point?pt?
          = ?getSize(east,?flushCache);
          ????????????heightOther?
          = ?Math.max(heightOther,?pt.y);
          ????????}
          ????????height?
          += ?heightOther;

          ???????? return ? new ?Point(Math.max(width,?wHint),?Math.max(height,?hHint));
          ????}

          computeSize計算Composite所需的大小。

          ???? protected ? void ?layout(Composite?composite,? boolean ?flushCache)?{
          ????????getControls(composite);
          ????????Rectangle?rect?
          = ?composite.getClientArea();
          ????????
          int ?left? = ?rect.x,?right? = ?rect.width,?top? = ?rect.y,?bottom? = ?rect.height;
          ????????
          if ?(north? != ? null )?{
          ????????????Point?pt?
          = ?getSize(north,?flushCache);
          ????????????north.setBounds(left,?top,?rect.width,?pt.y);
          ????????????top?
          += ?pt.y;
          ????????}
          ????????
          if ?(south? != ? null )?{
          ????????????Point?pt?
          = ?getSize(south,?flushCache);
          ????????????south.setBounds(left,?rect.height?
          - ?pt.y,?rect.width,?pt.y);
          ????????????bottom?
          -= ?pt.y;
          ????????}
          ????????
          if ?(east? != ? null )?{
          ????????????Point?pt?
          = ?getSize(east,?flushCache);
          ????????????east.setBounds(rect.width?
          - ?pt.x,?top,?pt.x,?(bottom? - ?top));
          ????????????right?
          -= ?pt.x;
          ????????}
          ????????
          if ?(west? != ? null )?{
          ????????????Point?pt?
          = ?getSize(west,?flushCache);
          ????????????west.setBounds(left,?top,?pt.x,?(bottom?
          - ?top));
          ????????????left?
          += ?pt.x;
          ????????}
          ????????
          if ?(center? != ? null )?{
          ????????????center.setBounds(left,?top,?(right?
          - ?left),?(bottom? - ?top));
          ????????}
          ????}

          而layout方法讓控件們各歸其位。整個布局調(diào)用是回歸的。

          完整的代碼borderlayout.rar

          上一篇

          posted on 2007-02-20 23:18 gr8vyguy 閱讀(3346) 評論(1)  編輯  收藏 所屬分類: Java

          評論

          # re: SWT中模擬AWT的BorderLayout 2007-02-22 06:06 BeanSoft

          支持一下, 最近也在苦學(xué) SWT, 實現(xiàn)一個自己用的資料管理軟件, 哥們真是好人!  回復(fù)  更多評論   

          <2007年2月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728123
          45678910

          導(dǎo)航

          統(tǒng)計

          公告

        1. 轉(zhuǎn)載請注明出處.
        2. msn: gr8vyguy at live.com
        3. 常用鏈接

          留言簿(9)

          隨筆分類(68)

          隨筆檔案(80)

          文章分類(1)

          My Open Source Projects

          搜索

          積分與排名

          最新評論

          主站蜘蛛池模板: 达拉特旗| 岗巴县| 蒙城县| 郑州市| 喜德县| 曲麻莱县| 土默特左旗| 翁牛特旗| 蒙阴县| 福泉市| 景泰县| 林口县| 郓城县| 巫溪县| 申扎县| 金堂县| 安溪县| 彭泽县| 乐清市| 三河市| 灵寿县| 库尔勒市| 乌恰县| 巨野县| 博野县| 宜都市| 杭锦后旗| 武宣县| 平原县| 商南县| 政和县| 大港区| 昌邑市| 新田县| 镇巴县| 望谟县| 射洪县| 马关县| 石门县| 荣成市| 梨树县|