EVENT HANDLING
Tugas
EventCheckBox.java
import java.awt.*;
import javax.swing.*;
import
java.awt.event.*;
class
CheckBoxHandling extends JFrame implements ItemListener {
private JTextField teks;
private JPanel panel;
private JCheckBox cb1, cb2, cb3;
public
CheckBoxHandling() {
setTitle("Coba CheckBox
Handling");
teks = new JTextField("Jenis Kelamin :
Pilihan");
teks.setHorizontalAlignment(SwingConstants.CENTER);
panel = new JPanel(new FlowLayout());
cb1 = new JCheckBox("Pria");
cb2 = new JCheckBox("Wanita");
cb3 = new JCheckBox("Banci");
ButtonGroup group = new ButtonGroup();
group.add(cb1);
group.add(cb2);
group.add(cb3);
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
setLayout(new BorderLayout());
add(teks, "North");
add(panel, "West");
panel.add(cb1);
panel.add(cb2);
panel.add(cb3);
pack();
setVisible(true);
setLocation(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void
itemStateChanged(ItemEvent event) {
if(event.getSource()==cb1) {
if(event.getStateChange()==ItemEvent.SELECTED)
teks.setText("Jenis Kelamin :
Pria");
}
if(event.getSource()==cb2) {
if(event.getStateChange()==ItemEvent.SELECTED)
teks.setText("Jenis Kelamin :
Wanita");
}
if(event.getSource()==cb3) {
if(event.getStateChange()==ItemEvent.SELECTED)
teks.setText("Jenis Kelamin :
Banci");
}
}
}
public class EventCheckBox {
public static void main (String[] args) {
new CheckBoxHandling();
}
}