TesGridBag.java (PemrogramanBerorientasi Objek)

TesGridBag.java



import java.awt.*;
import java.awt.event.*;

public class TesGridBag extends Frame {
  Button b1, b2, b3, b4, b5, b6, b7, b8;

  public static void main (String[] args) {
    TesGridBag apl = new TesGridBag();
    }

    public TesGridBag(){
      super("Tes GridBagLayout");
      setSize(300,250);

      GridBagLayout gb = new GridBagLayout();
      setLayout(gb);
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;

      //--- Baris Pertama
      buatTombol(b1, "Satu", gb, gbc,
                                0, 0, 2, 1);
      buatTombol(b2, "Dua", gb, gbc,
                                2,0,1,1);

      //--- Baris Kedua
      buatTombol(b3, "Tiga", gb, gbc,
                                0,1,1,1);
      buatTombol(b4, "empat", gb, gbc,
                                1,1,1,1);
      buatTombol(b5, "Lima", gb, gbc,
                                2,1,1,2);

      //---Baris ketiga
      buatTombol(b6, "enam", gb, gbc,
                                0,2,1,1);
      buatTombol(b7, "Tujuh", gb, gbc,
                                1,2,1,1);


      //---Baris empat
      buatTombol(b8, "Exit", gb, gbc,
                                0,4,3,1);


      addWindowListener(
                            new TesGridBag.PenanganKejadian());
                              show();
                              }




      public void buatTombol(Button b,
                                                    String judul,
                                                    GridBagLayout gb,
                                                    GridBagConstraints gbc,
                                                    int kolom, int baris,
                                                    int lebar, int tinggi) {
      b = new Button(judul);
      b.addActionListener(new PenangananTombol());
      gbc.gridx = kolom;
      gbc.gridy = baris;
      gbc.gridwidth = lebar;
      gbc.gridheight = tinggi;
      gb.setConstraints(b, gbc);
      add(b);
      }


class PenangananTombol implements ActionListener {
             public void actionPerformed(ActionEvent e) {
                String status = e.getActionCommand();
                if (status.equals("Exit")) {
                    System.exit(0);
                }
             }
}
class PenanganKejadian extends WindowAdapter {
                public void windowClosing(WindowEvent e) {
                                       System.exit(0);
                                       }
                        }
}


HASILNYA :


Jika
      gbc.fill = GridBagConstraints.BOTH;
diganti menjadi
      gbc.fill = GridBagConstraints.HORIZONTAL;

HASILNYA :



Jika
      gbc.fill = GridBagConstraints.BOTH;
diganti menjadi
      gbc.fill = GridBagConstraints.VERTICAL;

HASILNYA :


jika di klik tombol exit maka keluar ..

0 komentar:

Posting Komentar

Diberdayakan oleh Blogger.

Followers

Back to top