hafidh

JAVA GUI : Bekerja dengan checkbox

Nov 26th, 2011
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. // paket yang diperlukan
  2. import javax.swing.*;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5.  
  6. class KerjaCheckBox extends JFrame{
  7.     // attribut
  8.     //membuat tombol
  9.     JButton tombol1 = new JButton("cek ?");
  10.     JButton tombol2 = new JButton("set nilai true");
  11.     JLabel label = new JLabel("nilai check box");
  12.     // JCheckBox(String nama)
  13.     JCheckBox cb = new JCheckBox("check boxnya");
  14.    
  15.     //konstruktor
  16.     public KerjaCheckBox(){
  17.         //frame seting---------------------------------------------------------------------
  18.         //memanggil konstruktor kelas induk (JFrame)
  19.         super("Contoh Checkbox");
  20.         //seting besar frame 400 x 400 px
  21.         this.setSize(200,200);
  22.         //seting agar bisa ditutup
  23.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  24.         //seting kemunculan frame di tengah window
  25.         this.setLocationRelativeTo(null);
  26.         //seting layout frame
  27.         this.getContentPane().setLayout(null);
  28.         //frame seting---------------------------------------------------------------------
  29.         //menambahkan komponen ke frame dan seting letak
  30.         this.add(tombol1);
  31.         this.add(tombol2);
  32.         this.add(label);
  33.         this.add(cb);
  34.         // setbounds(x,y,lebar,tinggi)
  35.         tombol1.setBounds(10,10,150,25);
  36.         tombol2.setBounds(10,40,150,25);
  37.         label.setBounds(10,70,150,25);
  38.         cb.setBounds(10,110,150,25);
  39.         //seting aksi tombol
  40.         tombol1.addActionListener(new ActionListener( ) {
  41.             public void actionPerformed(ActionEvent e){
  42.                 // kode disini; cek keadaan check box
  43.                 if(cb.isSelected()) label.setText("CheckBox dicentang");
  44.                 else label.setText("CheckBox tidak dicentang");
  45.             }
  46.         });
  47.         tombol2.addActionListener(new ActionListener( ) {
  48.             public void actionPerformed(ActionEvent e){
  49.                 // kode disini; cek keadaan check box ganti keadaan chek box
  50.                 if(cb.isSelected()) cb.setSelected(false);
  51.                 else cb.setSelected(true);
  52.             }
  53.         });
  54.         // menampilkan frame
  55.         show();
  56.        
  57.     }
  58.    
  59.     public static void main(String[] args){
  60.         new KerjaCheckBox();
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment