Berikut ini program lengkapnya :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckBoxTest extends JFrame {
private JTextField text;
private JCheckBox bold, italic, merah;
public CheckBoxTest() {
super ("Mencoba CheckBox");
Container container = getContentPane ();
container.setLayout(new FlowLayout());
text = new JTextField ("Hello Java !", 20);
text.setFont(new Font ("Arial", Font.PLAIN, 14));
text.setForeground(Color.BLACK);
text.setBackground(Color.WHITE);
text.setEditable(false);
container.add(text);
bold = new JCheckBox ("Bold");
container.add(bold);
italic = new JCheckBox ("Italic");
container.add(italic);
merah = new JCheckBox ("Merah");
container.add(merah);
CheckBoxHandler atur = new CheckBoxHandler();
bold.addItemListener(atur);
italic.addItemListener(atur);
merah.addItemListener(atur);
setSize (275,100);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main (String args[]) {
CheckBoxTest test = new CheckBoxTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//inner class
private class CheckBoxHandler implements ItemListener {
private int valBold = Font.PLAIN;
private int valItalic = Font.PLAIN;
private Color valMerah = Color.BLACK;
//method untuk menangani checkbox event
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == bold) {
valBold = bold.isSelected() ? Font.BOLD : Font.PLAIN;
}
if (e.getSource() == italic) {
valItalic = italic.isSelected() ? Font.ITALIC : Font.PLAIN;
}
text.setFont(new Font ("Arial",valBold + valItalic, 14) );
if (e.getSource() == merah) {
valMerah = merah.isSelected() ? Color.RED : Color.BLACK;
}
text.setForeground(valMerah);
}
} //end of inner class
} //end of class CheckBoxTest
Semoga bermanfaat buat sobat sekalian,.
Sumber : http://contohprogram.info/




