EventCheck.java
kode:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CheckHandling extends JFrame implements ItemListener{
private JTextField teks;
private JPanel panel;
private JCheckBox CBox1, CBox2, CBox3;
public CheckHandling() {
setTitle("Tugas CheckBox Handling");
teks = new JTextField("Jenis Kelamin : Pilihan");
teks.setHorizontalAlignment(SwingConstants.CENTER);
panel = new JPanel(new FlowLayout());
CBox1 = new JCheckBox("Pria");
CBox2 = new JCheckBox("Wanita");
CBox3 = new JCheckBox("Banci");
ButtonGroup group = new ButtonGroup();
group.add(CBox1);
group.add(CBox2);
group.add(CBox3);
CBox1.addItemListener(this);
CBox2.addItemListener(this);
CBox3.addItemListener(this);
setLayout(new BorderLayout());
add(teks,"North");
add(panel,"West");
panel.add(CBox1);
panel.add(CBox2);
panel.add(CBox3);
pack();
setVisible(true);
setLocation(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent event) {
if(event.getSource()==CBox1) {
if(event.getStateChange()==ItemEvent.SELECTED)
teks.setText("Jenis Kelamin : Pria");
}
if(event.getSource()==CBox2){
if(event.getStateChange()==ItemEvent.SELECTED)
teks.setText("Jenis Kelamin : Wanita");
}
if(event.getSource()==CBox3){
if(event.getStateChange()==ItemEvent.SELECTED)
teks.setText("Jenis Kelamin : Banci");
}
}
}
public class EventCheck{
public static void main (String[] args){
new CheckHandling();
}
}
Output:
Tidak ada komentar:
Posting Komentar