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

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

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

    Java桌面技術

    Java Desktop Technology

    常用鏈接

    統(tǒng)計

    友情連接

    最新評論

    自定義布局管理器-CenterLayout

    上文自定義布局管理器-FormLayout介紹了FormLayout的參考實現(xiàn),利用FormLayout,通過指定left、top、

    right(可選)、bottom(可選)布局約束可以對組件進行精確定位。然而有些組件在業(yè)務上是有固定尺寸的,例如自定義組件之Button介紹的一樣,通過給按鈕指定4種狀態(tài)時的圖片,那么組件的最佳尺寸就是圖片的尺寸,因此組件的PreferredSize就可以確定,所以此時只需要組件中心的確定坐標就可以了,實際組件的Location只和其PreferredSize有關。如下圖所示:

    這就是CenterLayout的思想。

    修改FormData,只需要添加兩個變量即可。
    public final class FormData {
        public FormAttachment left;

        public FormAttachment right;

        public FormAttachment top;

        public FormAttachment bottom;

        public FormAttachment centerX;

        public FormAttachment centerY;
    }
    CenterLayout與FormLayout不同只在于addLayoutComponent、layoutContainer這兩個

    方法實現(xiàn),其他接口方法均相同,所以下面只介紹這兩個方法實現(xiàn),其他接口方法請

    參閱上文自定義布局管理器-FormLayout
    在addLayoutComponent方法的開頭,同樣是對布局約束參數(shù)constraints合法性進行檢查,這點與FormLayout大致相同。

    if (constraints == null) {
                throw new IllegalArgumentException("constraints can't be null");
            } else if (!(constraints instanceof FormData)) {
                throw new IllegalArgumentException("constraints must be a " + FormData.class.getName() + " instance");
            } else {
                synchronized (comp.getTreeLock()) {
                    FormData formData = (FormData) constraints;
                    if (formData.centerX == null || formData.centerY == null) {
                        throw new IllegalArgumentException("centerX FormAttachment and centerY FormAttachment can't be null");
                    } else if (comp.getPreferredSize() == null) {
                        throw new RuntimeException("component must have preferred size before be add into parent with CenterLayout");
                    }
                     componentConstraints.put(comp, (FormData) constraints);
                }
            }

    對于CenterLayout來說,F(xiàn)ormData對象的centerX、centerY必須給出,因為它代表,點的坐標,除此之外組件必須有PreferredSize屬性來指定組件大小。

    layoutContainer方法實現(xiàn)也大致相同
    public synchronized void layoutContainer(Container target) {
            synchronized (target.getTreeLock()) {
                int w = target.getWidth();
                int h = target.getHeight();
                Component[] components = target.getComponents();
                for (Component comp : components) {
                    FormData formData = componentConstraints.get(comp);
                    if (formData != null) {
                        ...
                    }
                }
            }
        }
    上面這步與FormLayout一樣。關鍵在if語句塊內,代碼如下:
      FormAttachment centerX = formData.centerX;
                        FormAttachment centerY = formData.centerY;
                        int width = component.getPreferredSize().width;
                        int height = component.getPreferredSize().height;
                        int x = (int) (centerX.percentage * w) + centerX.offset - width / 2;
                        int y = (int) (centerY.percentage * h) + centerY.offset - height / 2;
                        comp.setBounds(x, y, width, height);
    獲得centerX、centerY以及最佳尺寸,如上圖所示,不難得出x、y的計算方法。

    至此,自定義布局管理器就介紹到這里,這兩個布局類可以解決很多靜態(tài)布局需求,所謂靜態(tài)布局是指容器內有什么組件是固定的。如果遇到動態(tài)界面,例如組件的內容依照用戶級別、插件擴展點等因素決定,也并不是難事,因為了解了布局管理器運行機制以后可很容易地定義適合你需求的布局類。對于靜態(tài)布局來說,你可能厭倦了hard coding來布局,你希望這一切由xml這樣的配置搞定,好,下一部分則開始“壓軸戲”——用配置文件解決布局。

    posted on 2007-11-29 16:53 sun_java_studio@yahoo.com.cn(電玩) 閱讀(8234) 評論(1)  編輯  收藏 所屬分類: NetBeansGUI Design

    評論

    # re: 自定義布局管理器-CenterLayout 2008-07-01 10:57 java綜合網

    hao  回復  更多評論   

    TWaver中文社區(qū)
    主站蜘蛛池模板: 成全视频高清免费观看电视剧| 国产一卡二卡四卡免费| 亚洲精品在线免费观看视频| 亚洲福利在线视频| 日本高清免费不卡在线| 国产成人亚洲精品蜜芽影院| 色拍自拍亚洲综合图区| 222www免费视频| 一级一片免费视频播放| 亚洲免费综合色在线视频| 日韩精品无码人妻免费视频| 99精品免费观看| 亚洲熟妇丰满xxxxx| 免费在线观看黄网| 欧美在线看片A免费观看| 国产亚洲福利一区二区免费看| 亚洲精品国产啊女成拍色拍| 中文字幕人成人乱码亚洲电影| 成人午夜18免费看| av无码国产在线看免费网站| 最近中文字幕大全免费版在线| www一区二区www免费| 亚洲av丰满熟妇在线播放| 久久久久国产成人精品亚洲午夜 | 0588影视手机免费看片| 亚洲欧美国产精品专区久久| 亚洲综合视频在线观看| 亚洲成a人片在线观看无码专区| 亚洲无码精品浪潮| 亚洲精品成人网久久久久久| 国产精品免费视频一区| 三级黄色片免费看| 亚洲欧洲另类春色校园小说| 亚洲好看的理论片电影| 久久91亚洲精品中文字幕| 久久亚洲国产精品一区二区| 国产亚洲一区二区三区在线观看| 老司机在线免费视频| 中文字幕免费观看全部电影| www在线观看播放免费视频日本| 免费精品视频在线|