Pages

Minggu, 04 Desember 2011

Kajian 2 - JRadioButton


JRadioButton adalah komponen yang digunakan ketika pengguna perlu memilih satu diantara beberapa pilihan.

Berikut contoh sintaknya :
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class kom3 {
            public static void main(String[] args) {

                        JFrame j = new JFrame();
                        j.setSize(300, 120);
                        j.setLocation(200, 300);
                        j.setVisible(true);
                        j.setTitle("tes");
                        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                        JLabel l = new JLabel(" APAKAH ANDA SUKA PELAJARAN JAVA?");

                        JRadioButton[] r = new JRadioButton[4];
                        r[0] = new JRadioButton("SANGAT SUKA");
                        r[1] = new JRadioButton("SUKA");
                        r[2] = new JRadioButton("BIASA");
                        r[3] = new JRadioButton("TIDAK SUKA");

                        JPanel p = new JPanel();
                        p.add(l);

                        ButtonGroup b = new ButtonGroup();

                        for (int i = 0; i < r.length; i++) {
                                    b.add(r[i]);
                                    p.add(r[i]);
                        }
                        j.add(p);
                        j.setVisible(true);
                        JButton bt = new JButton("KLIK");
                        p.add(bt);

            }
}

0 komentar:

Posting Komentar