Berikut ini merupakan contoh kodingan penggunaan radio button pada bluej
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class Tugas1 extends JFrame{
// attribut
JButton tombol1 = new JButton("OK");
JButton tombol2 = new JButton("Cancel");
JLabel label1 = new JLabel("nama");
JLabel label2 = new JLabel("jenis kelamin");
JLabel label3 = new JLabel("alamat");
JTextField teks1 = new JTextField("");
JTextField teks2 = new JTextField("");
JRadioButton cb1 = new JRadioButton("Laki-laki");
JRadioButton cb2 = new JRadioButton("Perempuan");
ButtonGroup group = new ButtonGroup();
String jk;
//konstruktor
public Tugas1(){
//frame seting———————————————————————
//memanggil konstruktor kelas induk (JFrame)
super();
//seting besar frame 400 x 400 px
this.setSize(500,500);
//seting agar bisa ditutup
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//seting kemunculan frame di tengah window
this.setLocationRelativeTo(null);
//seting layout frame
this.getContentPane().setLayout(null);
//frame seting———————————————————————
//menambahkan komponen ke frame dan seting letak
this.add(tombol1);
this.add(tombol2);
this.add(label1);
this.add(label2);
this.add(label3);
this.add(teks1);
this.add(teks2);
this.add(cb1);
this.add(cb2);
// setbounds(x,y,lebar,tinggi)
tombol1.setBounds(10,110,100,25);
tombol2.setBounds(130,110,100,25);
label1.setBounds(10,10,150,25);
label2.setBounds(10,40,150,25);
label3.setBounds(10,70,150,25);
teks1.setBounds(110,10,150,25);
teks2.setBounds(110,70,150,25);
cb1.setBounds(110,40,100,25);
cb2.setBounds(210,40,150,25);
//seting aksi tombol
group.add(cb1);
group.add(cb2);
tombol1.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e){
// kode disini; cek keadaan check box
if(cb1.isSelected()){
jk="Laki-laki";
}else{
jk="Perempuan";
}
JOptionPane.showMessageDialog(null,teks1.getText()+"\n"+jk+"\n"+teks2.getText());
}
});
tombol2.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e){
// kode disini; cek keadaan check box ganti keadaan chek box
}
});
// menampilkan frame
show();
}
public static void main(String[] args){
new Tugas1();
}
}
dan di bawah ini merupakan hasil dari koding diatas
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class Tugas1 extends JFrame{
// attribut
JButton tombol1 = new JButton("OK");
JButton tombol2 = new JButton("Cancel");
JLabel label1 = new JLabel("nama");
JLabel label2 = new JLabel("jenis kelamin");
JLabel label3 = new JLabel("alamat");
JTextField teks1 = new JTextField("");
JTextField teks2 = new JTextField("");
JRadioButton cb1 = new JRadioButton("Laki-laki");
JRadioButton cb2 = new JRadioButton("Perempuan");
ButtonGroup group = new ButtonGroup();
String jk;
//konstruktor
public Tugas1(){
//frame seting———————————————————————
//memanggil konstruktor kelas induk (JFrame)
super();
//seting besar frame 400 x 400 px
this.setSize(500,500);
//seting agar bisa ditutup
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//seting kemunculan frame di tengah window
this.setLocationRelativeTo(null);
//seting layout frame
this.getContentPane().setLayout(null);
//frame seting———————————————————————
//menambahkan komponen ke frame dan seting letak
this.add(tombol1);
this.add(tombol2);
this.add(label1);
this.add(label2);
this.add(label3);
this.add(teks1);
this.add(teks2);
this.add(cb1);
this.add(cb2);
// setbounds(x,y,lebar,tinggi)
tombol1.setBounds(10,110,100,25);
tombol2.setBounds(130,110,100,25);
label1.setBounds(10,10,150,25);
label2.setBounds(10,40,150,25);
label3.setBounds(10,70,150,25);
teks1.setBounds(110,10,150,25);
teks2.setBounds(110,70,150,25);
cb1.setBounds(110,40,100,25);
cb2.setBounds(210,40,150,25);
//seting aksi tombol
group.add(cb1);
group.add(cb2);
tombol1.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e){
// kode disini; cek keadaan check box
if(cb1.isSelected()){
jk="Laki-laki";
}else{
jk="Perempuan";
}
JOptionPane.showMessageDialog(null,teks1.getText()+"\n"+jk+"\n"+teks2.getText());
}
});
tombol2.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e){
// kode disini; cek keadaan check box ganti keadaan chek box
}
});
// menampilkan frame
show();
}
public static void main(String[] args){
new Tugas1();
}
}
dan di bawah ini merupakan hasil dari koding diatas
Comments
Post a Comment