Pertemuan Container)

Embed Size (px)

Citation preview

  • 8/6/2019 Pertemuan Container)

    1/2

    Modul Pelatihan java Swing Asep Komarudin

    Container(JPanel)

    Jframe baru dapat diisi dengan komponen swing setelah diletakan

    kontainer.Kontainer merupakan komponen husus yang berguna untukmenampung komponen swing lainnya.

    Contohnya adalah:

    public class ContohContainer extends JFrame {

    public ContohContainer() {

    //membuat judul frame

    super("Frame dan button");

    //membuat ukuran frame

    setSize(350, 300);

    //pada saat buton close di klik maka java juga close

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //menempatkan frame ditengah-tengah

    setLocationRelativeTo(null);

    }

    /**

    * @param args the command line arguments

    */

    public static void main(String[] args) {

    // TODO code application logic here

    //memangil construktor class

    ContohContainer cc=new ContohContainer();

    //memangil construktor JPanel

    [email protected] Page 1

  • 8/6/2019 Pertemuan Container)

    2/2

    Modul Pelatihan java Swing Asep Komarudin

    JPanel panel=new JPanel();

    //merubah background panel

    panel.setBackground(Color.BLUE);

    //Memanggil constuktor buton ok

    JButton ButtonOk=new JButton("Ok");

    //memangil image icon pada directory

    ImageIcon icTool=new ImageIcon("/home/asep/Unduhan/lucu.gif");

    //memanggil consttruktor button

    JButton no=new JButton("Lucu", icTool);

    //memasiukan button pada panel(container)

    panel.add(ButtonOk);

    panel.add(no);

    //memasukan panel pada frame

    cc.add(panel);

    //menampilkan frame

    cc.setVisible(true);

    }

    }

    Keluarannya adalah:

    [email protected] Page 2