`

JGoodies Form参数详解

 
阅读更多

http://hi.baidu.com/whicss/blog/item/d1e7d819efbc1a76dbb4bd03.html

 

Alignments

JGoodies Forms 允許元件排列:水平 – left、center、right
                    垂直 –  top、center、bottom
並且可以填滿 (fill) 一個或多個cell之中有用的空間。
FormLayout layout = new FormLayout(
"left:pref, 15px, center:pref, 15px, right:pref, 15px, fill:pref,15px, pref",
"pref, 12px, pref, 4px, pref, 4px, pref, 4px, pref, 4px, pref");

上面的程式碼是告知元件在行與列中default的排列方式和大小。如果想改變元件的排列方式的話,可以利用CellConstraints的instance在元件加入時改變。


panel.add(new JLabel("Fill"),     cc.xy(7,  1, 
"center, center"
));  

panel.add(new JLabel("Default"), cc.xy(9, 1, "center, center" ));


Basic Sizes

Form提供了豐富的大小設定:75px 指的是固定75像素的大小,pref是元件最適當的大小。

 

private JComponent buildHorizontalSizesPanel() {
FormLayout layout = new FormLayout(
"pref, 12px, " + "75px, 25px, min, 25px, pref",
"pref, 12px, pref");

// Create a panel that uses the layout.
JPanel panel = new JPanel(layout);

// Set a default border.
panel.setBorder(Borders.DIALOG_BORDER);

// Create a reusable CellConstraints instance.
CellConstraints cc = new CellConstraints();

// Add components to the panel.
panel.add(new JLabel("75px"), cc.xy(3, 1));
panel.add(new JLabel("Min"), cc.xy(5, 1));
panel.add(new JLabel("Pref"), cc.xy(7, 1));

panel.add(new JLabel("new JTextField(15)"), cc.xy(1, 3));

panel.add(new JTextField(15), cc.xy(3, 3));
panel.add(new JTextField(15), cc.xy(5, 3));
panel.add(new JTextField(15), cc.xy(7, 3));

return panel;
}

上面利用"+"來區隔出不同的元件區塊,進而使程式碼不會太雜亂。

Growing

如果容器大於最適當的容器大小,會有額外的空間。這時就可以指明如何去分配這些額外的空間 ~ 讓單一的行或列擴張(growing),或是結合行列。

接著來看看這些程式碼片斷...

private JComponent buildHorizontalAllExtraSpacePanel() {
FormLayout layout = new FormLayout(
"pref, 6px, pref:grow",
"pref, 12px, pref");

JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();

panel.add(new JLabel("Fixed"), cc.xy(1, 1));
panel.add(new JLabel("Gets all extra space"), cc.xy(3, 1));

panel.add(new JTextField(5), cc.xy(1, 3));
panel.add(new JTextField(5), cc.xy(3, 3));

return panel;
}

在formLayout的參數中可以看到pref:grow 。表示說,在這個位置的元件,大小為元件本身的大小 (最合適的大小...pref ) ,並會隨著面版的縮放,進而縮小或放大填滿剩下的有用空間 ( grow )。

private JComponent buildHorizontalHalfAndHalfPanel() {
FormLayout layout = new FormLayout(
"pref, 6px, 0:grow , 6px, 0:grow ",
"pref, 12px, pref");

JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();

panel.add(new JLabel("Fixed"), cc.xy(1, 1));
panel.add(new JLabel("Gets half of extra space"), cc.xy(3, 1));
panel.add(new JLabel("gets half of extra space"), cc.xy(5, 1));

panel.add(new JTextField(5), cc.xy(1, 3));
panel.add(new JTextField(5), cc.xy(3, 3));
panel.add(new JTextField(5), cc.xy(5, 3));

return panel;
}

0:grow 表示在這個位置的元件,對齊方式是fill (default) ,元件大小為0,並且會擴張填滿目前剩下的有用空間。

下面二張圖可以看出pref:grow0:grow 的差別...



上面的圖是pref:grow ,下面的是0:grow 。可以發現到上面的圖第三個元件畫面被截掉了,這是因為第二個元件保有他自己本身的大小所致。

private JComponent buildHorizontalPercentMixedPanel() {
FormLayout layout = new FormLayout(
"pref, 6px, fill:0:grow(0.25) , 6px, fill:0:grow(0.75)",
"pref, 12px, pref");

JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();

panel.add(new JLabel("Fixed"), cc.xy(1, 1));
panel.add(new JLabel("Gets 25% of extra space"), cc.xy(3, 1));
panel.add(new JLabel("Gets 75% of extra space"), cc.xy(5, 1));

panel.add(new JTextField(5), cc.xy(1, 3));
panel.add(new JTextField(5), cc.xy(3, 3));
panel.add(new JTextField(5), cc.xy(5, 3));

return panel;
}

fill:0:grow(0.25)表示說:在這個位置的元件對齊方式是fill,元件大小為0,可以擴張的比列是:全部空間中的25%。

private JComponent buildHorizontalPercentPanel() {
FormLayout layout = new FormLayout(
" pref:grow(0.33) , 6px, pref:grow(0.67)",
"pref, 12px, pref");

JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();

panel.add(new JLabel("Gets 33% of the space"), cc.xy(1, 1));
panel.add(new JLabel("Gets 67% of the space"), cc.xy(3, 1));

panel.add(new JTextField(5), cc.xy(1, 3));
panel.add(new JTextField(5), cc.xy(3, 3));

return panel;
}

pref:grow(0.33)表示說:在這個位置的元件對齊方式是fill (default) ,元件大小為pref,可以縮放的比列是:剩下空間中的 33%。

 


 

fill:0:grow(x)pref:grow(x) 的不同處...






用相同的比例來看看,上面的圖使用的是fill:0:grow(0.25) ,下面的是pref:grow(0.25) 。上圖因為元件大小為0,所以直接取得整個空間中的25%(在先前元件之後的空間)。而下面的圖,因為保有基本的元件大小(pref),所以gorw只會取得...在所有 元件基本大小(pref)之後的額外空間(在這為25%)。也就是說,整個空間先減去所有 元件的基本大小,之後再從剩下空間中(若有的話)分配25%給此位置的元件,75%也是一樣。下面的圖說明一切...


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics