public class CheckBoxFrame extends JFrame
1: // Fig. 11.17: CheckBoxFrame.java
2: // Creating JCheckBox buttons.
3: import java.awt.FlowLayout;
4: import java.awt.Font;
5: import java.awt.event.ItemListener;
6: import java.awt.event.ItemEvent;
7: import javax.swing.JFrame;
8: import javax.swing.JTextField;
9: import javax.swing.JCheckBox;
10:
11: public class CheckBoxFrame extends JFrame
12: {
13: private JTextField textField; // displays text in changing fonts
14: private JCheckBox boldJCheckBox; // to select/deselect bold
15: private JCheckBox italicJCheckBox; // to select/deselect italic
16:
17: // CheckBoxFrame constructor adds JCheckBoxes to JFrame
18: public CheckBoxFrame()
19: {
20: super( "JCheckBox Test" );
21: setLayout( new FlowLayout() ); // set frame layout
22:
23: // set up JTextField and set its font
24: textField = new JTextField( "Watch the font style change", 20 );
25: textField.setFont( new Font( "Serif", Font.PLAIN, 14 ) );
26: add( textField ); // add textField to JFrame
27:
28: boldJCheckBox = new JCheckBox( "Bold" ); // create bold checkbox
29: italicJCheckBox = new JCheckBox( "Italic" ); // create italic
30: add( boldJCheckBox ); // add bold checkbox to JFrame
31: add( italicJCheckBox ); // add italic checkbox to JFrame
32:
33: // register listeners for JCheckBoxes
34: CheckBoxHandler handler = new CheckBoxHandler();
35: boldJCheckBox.addItemListener( handler );
36: italicJCheckBox.addItemListener( handler );
37: } // end CheckBoxFrame constructor
38:
39: // private inner class for ItemListener event handling
40: private class CheckBoxHandler implements ItemListener
41: {
42: private int valBold = Font.PLAIN; // controls bold font style
43: private int valItalic = Font.PLAIN; // controls italic font style
44:
45: // respond to checkbox events
46: public void itemStateChanged( ItemEvent event )
47: {
48: // process bold checkbox events
49: if ( event.getSource() == boldJCheckBox )
50: valBold =
51: boldJCheckBox.isSelected() ? Font.BOLD : Font.PLAIN;
52:
53: // process italic checkbox events
54: if ( event.getSource() == italicJCheckBox )
55: valItalic =
56: italicJCheckBox.isSelected() ? Font.ITALIC : Font.PLAIN;
57:
58: // set text field font
59: textField.setFont(
60: new Font( "Serif", valBold + valItalic, 14 ) );
61: } // end method itemStateChanged
62: } // end private inner class CheckBoxHandler
63: } // end class CheckBoxFrame
64:
65: /**************************************************************************
66: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
67: * Pearson Education, Inc. All Rights Reserved. *
68: * *
69: * DISCLAIMER: The authors and publisher of this book have used their *
70: * best efforts in preparing the book. These efforts include the *
71: * development, research, and testing of the theories and programs *
72: * to determine their effectiveness. The authors and publisher make *
73: * no warranty of any kind, expressed or implied, with regard to these *
74: * programs or to the documentation contained in these books. The authors *
75: * and publisher shall not be liable in any event for incidental or *
76: * consequential damages in connection with, or arising out of, the *
77: * furnishing, performance, or use of these programs. *
78: *************************************************************************/