hafidh

JAVA GUI : Test Radio Buttton

Dec 1st, 2011
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 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 TestRButton 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.     // JRadioButton(String nama)
  13.     JRadioButton cb = new JRadioButton("Makan");
  14.     JRadioButton cb2 = new JRadioButton("Ayam");
  15.     JRadioButton cb3 = new JRadioButton("goreng");
  16.     ButtonGroup group = new ButtonGroup();
  17.    
  18.     //konstruktor
  19.     public TestRButton(){
  20.         //frame seting---------------------------------------------------------------------
  21.         //memanggil konstruktor kelas induk (JFrame)
  22.         super("Contoh Tombol");
  23.         //seting besar frame 400 x 400 px
  24.         this.setSize(200,250);
  25.         //seting agar bisa ditutup
  26.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  27.         //seting kemunculan frame di tengah window
  28.         this.setLocationRelativeTo(null);
  29.         //seting layout frame
  30.         this.getContentPane().setLayout(null);
  31.         //frame seting---------------------------------------------------------------------
  32.         //menambahkan komponen ke frame dan seting letak
  33.         this.add(tombol1);
  34.         this.add(tombol2);
  35.         this.add(label);
  36.         this.add(cb);
  37.         this.add(cb2);
  38.         this.add(cb3);
  39.         // setbounds(x,y,lebar,tinggi)
  40.         tombol1.setBounds(10,10,150,25);
  41.         tombol2.setBounds(10,40,150,25);
  42.         label.setBounds(10,70,150,25);
  43.         cb.setBounds(10,110,150,25);
  44.         cb2.setBounds(10,140,150,25);
  45.         cb3.setBounds(10,170,150,25);
  46.         //seting aksi tombol
  47.         group.add(cb);
  48.         group.add(cb2);
  49.         group.add(cb3);
  50.         tombol1.addActionListener(new ActionListener( ) {
  51.             public void actionPerformed(ActionEvent e){
  52.                 // kode disini; cek keadaan check box
  53.                 if(cb.isSelected()) label.setText("Makan");
  54.                 if(cb2.isSelected()) label.setText("Ayam");
  55.                 if(cb3.isSelected()) label.setText("Goreng");
  56.             }
  57.         });
  58.         tombol2.addActionListener(new ActionListener( ) {
  59.             public void actionPerformed(ActionEvent e){
  60.                 // kode disini; cek keadaan check box ganti keadaan chek box
  61.                 if(cb.isSelected()) cb2.setSelected(true);
  62.                 else if(cb2.isSelected()) cb3.setSelected(true);
  63.                 else if(cb3.isSelected()) cb.setSelected(true);
  64.             }
  65.         });
  66.         // menampilkan frame
  67.         show();
  68.        
  69.     }
  70.    
  71.     public static void main(String[] args){
  72.         new TestRButton();
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment