GridBagLayout.docVIP

  • 0
  • 0
  • 约7.37千字
  • 约 8页
  • 2017-08-22 发布于江苏
  • 举报
GridBagLayout

Grid Bag Layout (Steven Shi,  HYPERLINK mailto:%20hi.steven@ hi.steven@ ,2002/05/18) Java提供的版面配置方式有FlowLayout、GridLayout、BorderLayout、CardLayout、BoxLayout及GridBagLayout,在這些Layout Manager裡,除了Grid Bag Layout外,都不需要輔助類別就可以搞定,唯獨Grid Bag Layout需要一個稱為GridBagConstraints的類別來設定其元件的各項屬性,所以Grid Bag Layout也就成為所有版面配置式中最複雜的一個。 底下先列出所有GridBagConstraints類別的欄位,再用一些samples來解釋Grid Bag Layout的使用方式。 欄位作用int anchor當元件小於其顯示區域時才用static int BOTH重新訂定元件之寬度及高度static int CENTER將元件置於顯示區域之中央位置static int EAST將元件置於顯示區域之右側位置,並於垂直做置中對齊int fill當顯示區域大於元件所需之大小才用此欄位int gridheight指定元件顯示區域之列數int gridwidth指定元件顯示區域之行數int gridx指定元件顯示區域左側之表格int gridy指定元件顯示區域上方之表格static int HORIZONTAL重新設定元件之水平位置Insets insets指定元件外側之間距int ipadx指定元件內側之間距xint ipady指定元件內側之間距ystatic int NONE表示元件之大小位置不重新設定static int NORTH將元件置於顯示區域之上方位置,並於水平做置中對齊static int NORTHEAST將元件置於顯示區域之右上角static int NORTHWEST將元件置於顯示區域之左上角static int RELATIVE指定元件為其行或列上最後一個元件之下一個元件,或上一次增加元件之下一個元件static int REMAINDER指定元件為其行或列上之最後一個元件static int SOUTH將元件置於顯示區域之下方位置,並於水平做置中對齊static int SOUTHEAST將元件置於顯示區域之右下方位置static int SOUTHWEST將元件置於顯示區域之左下方位置static int VERTICAL重新設定元件之垂直位置,但不設定其水平位置double weightx指定其它水平空間的配置方式double weighty指定其它垂直空間的配置方式static int WEST將元件置於顯示區域之左側位置,並於垂直做置中對齊 範列1: gridx、gridy gridx與gridy是用以指定元件的相對位置,由範例1可以看出當gridx及gridy的值變化時,對於Button的位置的改變,gridx和gridy的值是相對的,也就是說gridx值越大的排在越右邊,gridy越大的排在越下面。 import java.awt.*; public class Sample1 extends Frame { public Sample1() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints cons = new GridBagConstraints(); setLayout(gridbag); cons.gridy = 3; Button btn1 = new Button(Button1); gridbag.setConstraints(btn1, cons); this.add(btn1); cons.gridy = 2; cons.gridx = 4; Button btn2 = new Button(Button2); gridbag.setConstraints(btn2, cons); this.add(btn2); cons.gridy = 2; cons.gridx = 1; Button btn3 = new Button(Button3); gridbag.setConstraints(btn3, cons); this.a

文档评论(0)

1亿VIP精品文档

相关文档