自定義布局管理器-FormLayout
第二部分:自定義布局管理器
在java.awt包與javax.swing包下有許多現(xiàn)成的布局類,比如BorderLayout、FlowLayout,還有較為復(fù)雜的、用于精確定位的布局類GridBagLayout、SpringLayout等。起初我剛剛從事gooey時(shí)(06年中),企圖依靠JDK自帶的布局類進(jìn)行布局,但是實(shí)際不可能或者說很難做到。對于復(fù)雜的GridBagLayout、SpringLayout來說又望而生畏,而且使用GridBagLayout、SpringLayout來完成布局的話工作量相當(dāng)可觀,因此當(dāng)時(shí)放棄了布局管理器,采用ComponentListener等尺寸監(jiān)聽事件來布局組件。雖然這種做法沒有工具支持、采用手工coding,但是自由度上升了很多,而且熟悉了以后編碼效率也大幅其高。與此同時(shí),我開始接觸SWT,發(fā)現(xiàn)org.eclipse.swt.layout.FormLayout布局很強(qiáng)大、用起來愛不釋手、要多好有多好、要多強(qiáng)有多強(qiáng)......。于是當(dāng)時(shí)我用來布局組件的方式是采用ComponentListener監(jiān)聽與FormLayout結(jié)合的方式,也是在同期,我領(lǐng)悟到了九宮圖這種專業(yè)布局,因此之后九宮圖的實(shí)現(xiàn)也都采用上述兩種方法。隨著對SWT的不斷了解外加IM軟件界面的專業(yè)性,我發(fā)現(xiàn)SWT并不非常適合做專業(yè)外觀,也因?yàn)榇宋抑饾u將精力轉(zhuǎn)向Swing。
在介紹如何編寫自定義布局管理器前,我想先把SWT體系下的FormLayout布局(表單布局)特點(diǎn)做個(gè)簡要介紹。
SWT體系下的FormLayout是非常靈活、精確的布局,F(xiàn)ormLayout布局組件的特點(diǎn)是采用百分比+偏移量的方式。前者可以應(yīng)付容器尺寸變化時(shí)內(nèi)部組件隨之等比例調(diào)整;后者以應(yīng)付精確的布局。這一特征是通過org.eclipse.swt.layout.FormData和org.eclipse.swt.layout.FormAttachment兩個(gè)類來實(shí)現(xiàn)。
通常使用FormLayout來定位一個(gè)組件要確定4個(gè)FormAttachment對象:top、bottom、left、right,即組件的4條邊。而且通常是使用FormAttachment(int numerator,int offset)這個(gè)構(gòu)造器,也就是百分比+偏移量。當(dāng)然FormAttachment不只這一種,但是都是可選的,如果想深入研究FormLayout可以參閱SWT相關(guān)的介紹。
下面給出一段SWT示例程序:
運(yùn)行效果如下:

由運(yùn)行效果可以看出,F(xiàn)ormLayout通過指定組件的四條邊來完成布局。
FormLayout很強(qiáng)大、靈活,但是AWT、Swing包中卻沒有,但是不等于說不能實(shí)現(xiàn),學(xué)習(xí)了上文之后當(dāng)然可以移植到Swing中來。
SWT中使用FormLayout還要結(jié)合FormData(表單數(shù)據(jù))與FormAttachment(表單附件)。下面給出這兩個(gè)移植過來的類實(shí)現(xiàn)
你應(yīng)該了解坐標(biāo)系的概念,Java中的坐標(biāo)系以向右、向下為正方向。因此對于offset,正值是向右、向下偏移;負(fù)值是向左、向上偏移。
在FormLayout布局中,定位一個(gè)組件需要最多4個(gè)FormAttachment對象,但是可以不必全部指定,稍后可以看到缺省的行為。
如果你的布局管理器比較簡單,可以實(shí)現(xiàn)LayoutManager接口。但是正如上文所述,LayoutManager的addLayoutComponent(String name, Component comp)方法是必須通過java.awt.Container類的“Component add(String name, Component comp)”方法觸發(fā)調(diào)用,其中的字符串參數(shù)指定了布局信息。但是字符串表達(dá)方式很有限,因此應(yīng)當(dāng)采用LayoutManager2接口,這樣,addLayoutComponent(Component comp, Object constraints)方法被調(diào)用時(shí),“Object constraints”可以是任何類型的對象,很方便。下面逐步實(shí)現(xiàn)這個(gè)類。
首先搭建的原型如下
public final class FormLayout implements LayoutManager2 {
public void addLayoutComponent(Component comp, Object constraints) {}
public float getLayoutAlignmentX(Container target) {
return 0;
}
public float getLayoutAlignmentY(Container target) {
return 0;
}
public void invalidateLayout(Container target) {}
public Dimension maximumLayoutSize(Container target) {
return null;
}
public void addLayoutComponent(String name, Component comp) {}
public void layoutContainer(Container parent) {}
public Dimension minimumLayoutSize(Container parent) {
return null;
}
public Dimension preferredLayoutSize(Container parent) {
return null;
}
public void removeLayoutComponent(Component comp) {}
}
再聲明一個(gè)保存組件與布局信息對應(yīng)關(guān)系的映射:private final Map<Component, FormData> componentConstraints = new HashMap<Component, FormData>();
getContentPane().setLayout(new FormLayout());
JButton button = new JButton();
button.setText("button");
FormData formData = new FormData();
formData.top = new FormAttachment(0.2f, 0);
formData.left = new FormAttachment(0.5f, 0);
formData.bottom = new FormAttachment(0.2f, 30);
formData.right = new FormAttachment(0.5f, 50);
getContentPane().add(button,formData);
如上所示,當(dāng)調(diào)用“getContentPane().add(button,formData);”時(shí),布局類的 public void addLayoutComponent(Component comp, Object constraints)方法便會(huì)調(diào)用,constraints參數(shù)就是FormData對象。所以在addLayoutComponent方法中需要做的就是把組件與布局信息關(guān)聯(lián)起來。下面是完整實(shí)現(xiàn):
}
對于addLayoutComponent(String name, Component comp)方法,由于通過查看源碼發(fā)現(xiàn)“實(shí)現(xiàn)了LayoutManager2 接口的布局類該方法永遠(yuǎn)不會(huì)被調(diào)用”(未來的JDK版本如何實(shí)現(xiàn)不能保證),所以該方法空實(shí)現(xiàn),并在注視上作@deprecated標(biāo)記。
/**
* @deprecated
*/
public void addLayoutComponent(String name, Component comp) {}
除了layoutContainer方法,其余方法均很簡單。一并給出:
public float getLayoutAlignmentX(Container target) {
return 0.5f;
}
public float getLayoutAlignmentY(Container target) {
return 0.5f;
}
public void invalidateLayout(Container target) {}
public Dimension maximumLayoutSize(Container target) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
public Dimension minimumLayoutSize(Container target) {
return new Dimension(0, 0);
}
public Dimension preferredLayoutSize(Container target) {
return new Dimension(0, 0);
}
public void removeLayoutComponent(Component comp) {
synchronized (comp.getTreeLock()) {
componentConstraints.remove(comp);
}
}
根據(jù)上文所述,這些方法不難理解。其實(shí)對于FormLayout來說,...LayoutSize(Container target)、getLayoutAlignmentX等方法不是很重要。重要的是public void layoutContainer(Container target)的實(shí)現(xiàn),也是所有布局類最重要的一個(gè)類。
首先該方法的第一步也要套上 synchronized (target.getTreeLock()) {},所有的代碼放入同步塊中。接下來是:
final FormData formData = componentConstraints.get(comp);
if (formData == null) {
continue;
}
因?yàn)樵赼ddLayoutComponent(Component comp, Object constraints)方法中已經(jīng)關(guān)聯(lián)了組件與布局信息,所以可以通過componentMap.get(comp)這一行得到組件的布局信息,加上空值判斷確保代碼萬無一失。
接下來取出4個(gè)FormAttachment對象,表示組件的四條邊。
然后計(jì)算Location信息(組件左上角的坐標(biāo))x、y:
然后計(jì)算組件的長、高,width、height:
計(jì)算時(shí)根據(jù)給出right與bottom布局分為兩種情況,如果未給出,那么根據(jù)組件的getPreferredSize方法得到組件的最佳大小,以這個(gè)大小決定組件的尺寸。作為規(guī)范,使用布局管理器布局不是參照組件的getSize而是參照getPreferredSize來最終決定組件的尺寸,所有布局管理器也都是這么實(shí)現(xiàn)的。所以如果你企圖
設(shè)置組件的setSize()方法來達(dá)到在布局管理器中布局的目的是不可能的,所以你應(yīng)該視圖調(diào)用組件的setPreferredSize方法。接上,如果right和bottom都不是null,那么計(jì)算組件尺寸將忽略getPreferredSize,計(jì)算x2和y2的坐標(biāo),然后兩坐標(biāo)相減得到長寬。最后調(diào)用組件的setBounds進(jìn)行最終定位。
作為FormLayout需要補(bǔ)充的是,在進(jìn)行最終布局“component.setBounds(x, y, width, height);”之前,未進(jìn)行邏輯判斷,所以x、y可能會(huì)超出了容器的范圍而width、height也可能是負(fù)值,這都會(huì)導(dǎo)致組件“莫名其妙”地不可見,這都不是布局管理器的問題。例如以下兩行代碼:
formData.left = new FormAttachment(0.5f, 30);
formData.right = new FormAttachment(0.5f, 20);
就會(huì)使組件永遠(yuǎn)不能顯示,因?yàn)閷τ趌eft的定位,是位于容器50%處向右30像素處,而right是位于容器50%處向右20像素處,這樣組件的長度就是-10,怎么能顯示出來呢?
FormLayout就介紹到這里,因?yàn)榘l(fā)帖只能在周末,加上最近一段時(shí)間還有別的事,能擠出一點(diǎn)時(shí)間真不容易。請關(guān)注下一篇CenterLayout的實(shí)現(xiàn)。
posted on 2007-11-24 18:26 sun_java_studio@yahoo.com.cn(電玩) 閱讀(16907) 評論(6) 編輯 收藏 所屬分類: NetBeans 、GUI Design