<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    gr8vyguy@Blogjava

    SWT中模擬AWT的BorderLayout

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

    borderlayout.jpg

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

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

    要定義一個新的 layout ,也就是要定義一個 Layout 的子類, 實現 computeSize layout. BorderLayout 來說,我們需要區分子控件是在哪個位置的 , 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()方法設定, 所以getControl()方法找著各個控件的相應位置。

    ???? 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方法讓控件們各歸其位。整個布局調用是回歸的。

    完整的代碼borderlayout.rar

    上一篇

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

    評論

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

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

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

    導航

    統計

    公告

  • 轉載請注明出處.
  • msn: gr8vyguy at live.com
  • 常用鏈接

    留言簿(9)

    隨筆分類(68)

    隨筆檔案(80)

    文章分類(1)

    My Open Source Projects

    搜索

    積分與排名

    最新評論

    主站蜘蛛池模板: 久久国产免费一区| 456亚洲人成影院在线观| 国产成人免费片在线观看| 午夜网站免费版在线观看| 免费又黄又爽又猛的毛片| 亚洲爆乳精品无码一区二区三区 | 亚洲AV无码专区在线亚| 偷自拍亚洲视频在线观看| 国产在线观看无码免费视频| 国产精品久久免费| 亚洲第一视频在线观看免费| 亚洲天堂视频在线观看| 亚洲成av人片天堂网无码】| 免费在线看黄的网站| 国产一级一片免费播放i| 亚洲狠狠ady亚洲精品大秀| 欧洲美女大片免费播放器视频| 一级毛片在线观看免费| 亚洲片国产一区一级在线观看 | 亚洲色大成网站www永久网站| 免费无码一区二区三区蜜桃| 国产精品va无码免费麻豆| 免费的黄色网页在线免费观看| 相泽亚洲一区中文字幕| 亚洲色成人四虎在线观看| 免费播放春色aⅴ视频| 你懂的在线免费观看| 亚洲中文字幕视频国产| 色偷偷尼玛图亚洲综合| 国产免费毛不卡片| 亚洲成av人片在线观看无码不卡| 久久中文字幕免费视频| 一区二区亚洲精品精华液| 亚洲日本韩国在线| 在线人成精品免费视频| 亚洲乱亚洲乱妇无码麻豆| 美国免费高清一级毛片| 亚洲av最新在线网址| 精品国产免费一区二区| 2017亚洲男人天堂一| 国产精品亚洲αv天堂无码|