Swing : Layout Manager #3 - GridBagLayout
//GridBagLayout adalah layout manager yang mengatur komposisi
//dari komponen dengan aturan dasar grid tetapi lebih fleksibel
//Dengan GrigBagLayout anda dapat menyusun komponen tidak
//harus dengan aturan rata kiri-kanan atau dalam acuan 1 kolum,dsb
//Anda memerlukan class GridBagConstraints sebagai pengatur posisi
private GridBagLayout alayout=null;
private GridBagConstraints aconstraint=null;
public UsingGridBagLayout(){
super();
init();
}
public void init(){
alayout = new GridBagLayout();
aconstraint = new GridBagConstraints();
this.setTitle("Contoh Menggunakan GridBagLayout");
this.setSize(400,400);
this.getContentPane().setLayout(alayout);
//GridBagConstraints.BOTH - menresize komponen secara
//vertikal dan horizontal
aconstraint.fill=GridBagConstraints.BOTH;
JButton button1 = new JButton("Tombol 1");
JButton button2 = new JButton("Tombol 2");
JButton button3 = new JButton("Tombol 3");
JButton button4 = new JButton("Tombol 4");
//men-set constraint ke setiap button
alayout.setConstraints(button1,aconstraint);
alayout.setConstraints(button2,aconstraint);
alayout.setConstraints(button3,aconstraint);
this.getContentPane().add(button1);
this.getContentPane().add(button2);
this.getContentPane().add(button3);
//GridBagConstraints.REMAINDER - menentukan bahwa komponen
//yang diset adalah komponen terakhir, bila ada
//komponen selanjutnya
//maka komponen baru tersebut diletakkan di baris baru.
aconstraint.gridwidth=GridBagConstraints.REMAINDER;
alayout.setConstraints(button4,aconstraint);
this.getContentPane().add(button4);
//gridheight digunakan untuk menentukan jumlah baris
// yang "diambil" oleh komponen
//gridwidth digunakan untuk menentukan jumlah kolom
// yang "diambil" oleh komponen
aconstraint.gridheight=2;
aconstraint.gridwidth=1;
aconstraint.weighty=1.0;
JButton button5 = new JButton("Tombol 5");
alayout.setConstraints(button5,aconstraint);
this.getContentPane().add(button5);
aconstraint.gridheight=1;
aconstraint.weighty=0.0;
aconstraint.gridwidth=GridBagConstraints.REMAINDER;
JButton button6 = new JButton("Tombol 6");
JButton button7 = new JButton("Tombol 7");
alayout.setConstraints(button6,aconstraint);
alayout.setConstraints(button7,aconstraint);
this.getContentPane().add(button6);
this.getContentPane().add(button7);
}
public static void main(String[] args) {
UsingGridBagLayout aframe = new UsingGridBagLayout();
aframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}
});
aframe.pack();
aframe.show();
}

0 Comments:
Post a Comment
<< Home