public class RadioButtonFrame extends JFrame
1: // Fig. 11.19: RadioButtonFrame.java
2: // Creating radio buttons using ButtonGroup and JRadioButton.
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.JRadioButton;
10: import javax.swing.ButtonGroup;
11:
12: public class RadioButtonFrame extends JFrame
13: {
14: private JTextField textField; // used to display font changes
15: private Font plainFont; // font for plain text
16: private Font boldFont; // font for bold text
17: private Font italicFont; // font for italic text
18: private Font boldItalicFont; // font for bold and italic text
19: private JRadioButton plainJRadioButton; // selects plain text
20: private JRadioButton boldJRadioButton; // selects bold text
21: private JRadioButton italicJRadioButton; // selects italic text
22: private JRadioButton boldItalicJRadioButton; // bold and italic
23: private ButtonGroup radioGroup; // buttongroup to hold radio buttons
24:
25: // RadioButtonFrame constructor adds JRadioButtons to JFrame
26: public RadioButtonFrame()
27: {
28: super( "RadioButton Test" );
29: setLayout( new FlowLayout() ); // set frame layout
30:
31: textField = new JTextField( "Watch the font style change", 25 );
32: add( textField ); // add textField to JFrame
33:
34: // create radio buttons
35: plainJRadioButton = new JRadioButton( "Plain", true );
36: boldJRadioButton = new JRadioButton( "Bold", false );
37: italicJRadioButton = new JRadioButton( "Italic", false );
38: boldItalicJRadioButton = new JRadioButton( "Bold/Italic", false );
39: add( plainJRadioButton ); // add plain button to JFrame
40: add( boldJRadioButton ); // add bold button to JFrame
41: add( italicJRadioButton ); // add italic button to JFrame
42: add( boldItalicJRadioButton ); // add bold and italic button
43:
44: // create logical relationship between JRadioButtons
45: radioGroup = new ButtonGroup(); // create ButtonGroup
46: radioGroup.add( plainJRadioButton ); // add plain to group
47: radioGroup.add( boldJRadioButton ); // add bold to group
48: radioGroup.add( italicJRadioButton ); // add italic to group
49: radioGroup.add( boldItalicJRadioButton ); // add bold and italic
50:
51: // create font objects
52: plainFont = new Font( "Serif", Font.PLAIN, 14 );
53: boldFont = new Font( "Serif", Font.BOLD, 14 );
54: italicFont = new Font( "Serif", Font.ITALIC, 14 );
55: boldItalicFont = new Font( "Serif", Font.BOLD + Font.ITALIC, 14 );
56: textField.setFont( plainFont ); // set initial font to plain
57:
58: // register events for JRadioButtons
59: plainJRadioButton.addItemListener(
60: new RadioButtonHandler( plainFont ) );
61: boldJRadioButton.addItemListener(
62: new RadioButtonHandler( boldFont ) );
63: italicJRadioButton.addItemListener(
64: new RadioButtonHandler( italicFont ) );
65: boldItalicJRadioButton.addItemListener(
66: new RadioButtonHandler( boldItalicFont ) );
67: } // end RadioButtonFrame constructor
68:
69: // private inner class to handle radio button events
70: private class RadioButtonHandler implements ItemListener
71: {
72: private Font font; // font associated with this listener
73:
74: public RadioButtonHandler( Font f )
75: {
76: font = f; // set the font of this listener
77: } // end constructor RadioButtonHandler
78:
79: // handle radio button events
80: public void itemStateChanged( ItemEvent event )
81: {
82: textField.setFont( font ); // set font of textField
83: } // end method itemStateChanged
84: } // end private inner class RadioButtonHandler
85: } // end class RadioButtonFrame
86:
87: /**************************************************************************
88: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
89: * Pearson Education, Inc. All Rights Reserved. *
90: * *
91: * DISCLAIMER: The authors and publisher of this book have used their *
92: * best efforts in preparing the book. These efforts include the *
93: * development, research, and testing of the theories and programs *
94: * to determine their effectiveness. The authors and publisher make *
95: * no warranty of any kind, expressed or implied, with regard to these *
96: * programs or to the documentation contained in these books. The authors *
97: * and publisher shall not be liable in any event for incidental or *
98: * consequential damages in connection with, or arising out of, the *
99: * furnishing, performance, or use of these programs. *
100: *************************************************************************/