Selasa, 14 Agustus 2012
Demo Check Box
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DemoCheckBox implements ItemListener {
public JLabel label1;
public JCheckBox checkBox1, checkBox2, checkBox3;
private JFrame frame;
public DemoCheckBox() {
label1 = new JLabel("Pilihan:");
label1.setLocation(10, 10);
label1.setSize(label1.getPreferredSize());
checkBox1 = new JCheckBox("C", true);
checkBox1.setLocation(10, 25);
checkBox1.addItemListener(this);
checkBox1.setSize(checkBox1.getPreferredSize());
checkBox2 = new JCheckBox("C++", true);
checkBox2.setLocation(10, 50);
checkBox2.addItemListener(this);
checkBox2.setSize(checkBox2.getPreferredSize());
checkBox3 = new JCheckBox("Java", true);
checkBox3.setLocation(10, 75);
checkBox3.addItemListener(this);
checkBox3.setSize(checkBox3.getPreferredSize());
}
public void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("Demo JCheckBox");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label1);
frame.getContentPane().add(checkBox1);
frame.getContentPane().add(checkBox2);
frame.getContentPane().add(checkBox3);
frame.setBounds(0, 0, 300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void itemStateChanged(ItemEvent event) {
JCheckBox cb = (JCheckBox) event.getSource();
if (event.getStateChange() == ItemEvent.SELECTED) {
JOptionPane.showMessageDialog(frame,
"Anda telah memilih: " + cb.getText());
} else {
JOptionPane.showMessageDialog(frame,
"Anda telah telah melepas pilihan: " + cb.getText());
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
DemoCheckBox app = new DemoCheckBox();
app.createAndShowGUI();
}
});
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar