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.add(btn3);
cons.gridy = 4;
Button btn4 = new Button("Button4");
gridbag.setConstraints(btn4, cons);
this.add(btn4);
this.setSize(300, 500);
this.pack();
this.show();
}
public static void main(String[] args) {
Sample1 sample11 = new Sample1();
}
}
Sample1執行結果
範例2: gridwidth、gridheight、fill
gridwidth和gridheight是用來指定元件的相對大小,由範例2可以看出,當gridwidth和gridheight變化時,元件大小的變化。cons.fill = GridBagConstraints.BOTH是指元件將剩餘空間填滿的意思。
import java.awt.*;
public class Sample2 extends Frame {
public Sample2() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
setLayout(gridbag);
cons.fill = GridBagConstraints.BOTH;
cons.gridy = 1;
cons.gridwidth = 1;
Button btn1 = new Button("Button1");
gridbag.setConstraints(btn1, cons);
this.add(btn1);
cons.gridwidth = 2;
Button btn2 = new Button("Button2");
gridbag.setConstraints(btn2, cons);
this.add(btn2);
cons.gridy = 2;
cons.gridwidth = 1;
Button btn3 = new Button("Button3");
gridbag.setConstraints(btn3, cons);
this.add(btn3);
Button btn4 = new Button("Button4");
gridbag.setConstraints(btn4, cons);
this.add(btn4);
Button btn5 = new Button("Button5");
gridbag.setConstraints(btn5, cons);
this.add(btn5);
cons.gridy = 1;
cons.gridheight = 2;
Button btn6 = new Button("Button6");
gridbag.setConstraints(btn6, cons);
this.add(btn6);
this.pack();
this.show();
}
public static void main(String[] args) {
Sample2 sample21 = new Sample2();
}
}
Sample2執行結果
範例3: weightx、wieghty
weightx、weighty設定的是權值,是當外框大小改變時,其內元件變化的倍數值,當weightx、weighty值為0時,元件不會改變大小,如果值大於0則與其它也大於0的元件比較後,依權值大小決定變化的程度。
試著改變weightx、weighty並在程式執行時,透過拖曳改變Frame大小,就可看出weightx、weighty對元件大小的影響。
import java.awt.*;
public class Sample3 extends Frame {
public Sample3() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
setLayout(gridbag);
cons.fill = GridBagConstraints.BOTH;
//cons.weightx = 1;
cons.weighty = 1;
cons.anchor = GridBagConstraints.EAST;
cons.gridy = 1;
cons.gridx = 1;
Button btn1 = new Button("Button1");
gridbag.setConstraints(btn1, cons);
this.add(btn1);
cons.gridy = 1;
cons.gridx = 2;
Button btn2 = new Button("Button2");
gridbag.setConstraints(btn2, cons);
this.add(btn2);
cons.gridy = 1;
cons.gridx = 3;
Button btn3 = new Button("Button3");
gridbag.setConstraints(btn3, cons);
this.add(btn3);
cons.gridy = 2;
cons.gridx = 1;
cons.weighty = 0.5;
Button btn4 = new Button("Button4");
gridbag.setConstraints(btn4, cons);
this.add(btn4);
cons.gridy = 2;
cons.gridx = 2;
cons.weighty = 0;
Button btn5 = new Button("Button5");
gridbag.setConstraints(btn5, cons);
this.add(btn5);
cons.gridy = 3;
cons.gridx = 1;
Button btn6 = new Button("Button6");
gridbag.setConstraints(btn6, cons);
this.add(btn6);
this.setSize(300, 500);
this.pack();
this.show();
}
public static void main(String[] args) {
Sample3 sample31 = new Sample3();
}
}
Sample3執行結果
Sample3拖曳後的結果
範例4: anchor
anchor是用以指定在指定範圍裡,如果有空間,元件要靠那邊,可指定的值有GridBagConstraints.EAST、GridBagConstraints.WEST、GridBagConstraints.SOUTH、GridBagConstraints.NORTH及GridBagConstraints.CENTER。
試著改變範例4的anchor值,就可發現Button3的位置的改變。
import java.awt.*;
public class Sample4 extends Frame {
public Sample4() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
setLayout(gridbag);
cons.gridy = 1;
cons.gridx = 1;
cons.gridwidth = 1;
cons.gridheight = 1;
Button btn1 = new Button("Button1");
gridbag.setConstraints(btn1, cons);
this.add(btn1);
cons.gridy = 1;
cons.gridx = 2;
cons.gridwidth = 2;
Button btn2 = new Button("Button2");
gridbag.setConstraints(btn2, cons);
this.add(btn2);
//cons.anchor = GridBagConstraints.SOUTH;
cons.anchor = GridBagConstraints.NORTH;
cons.gridy = 1;
cons.gridx = 4;
cons.gridheight = 2;
cons.gridwidth = 1;
Button btn3 = new Button("Button3");
gridbag.setConstraints(btn3, cons);
this.add(btn3);
cons.gridy = 2;
cons.gridx = 2;
cons.gridheight = 1;
Button btn4 = new Button("Button4");
gridbag.setConstraints(btn4, cons);
this.add(btn4);
this.pack();
this.show();
}
public static void main(String[] args) {
Sample4 sample41 = new Sample4();
}
}
Sample4執行結果 (cons.anchor = GridBagConstraints.NORTH)
Sample4執行結果 (cons.anchor = GridBagConstraints. SOUTH)
範例5: ipadx、ipady
ipadx、ipady是用以指定元件最小寬度、高度外,要再加多少? 當指定了ipadx、ipady後,元件的寬度會是最小寬度加2*ipadx,高度會是最小高度加2*ipady,單位是pixel。
import java.awt.*;
public class Sample5 extends Frame {
public Sample5() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
setLayout(gridbag);
Button btn1 = new Button("Button1");
gridbag.setConstraints(btn1, cons);
this.add(btn1);
cons.ipadx = 100;
cons.ipady = 50;
Button btn2 = new Button("Button2");
gridbag.setConstraints(btn2, cons);
this.add(btn2);
this.pack();
this.show();
}
public static void main(String[] args) {
Sample5 sample51 = new Sample5();
}
}