Chapter 12 Graphical User Interface Components: Part 1 (Web design online)
Chapter 12 Graphical User Interface Components: Part 1 669 38 39 italicButton = new JRadioButton( “Italic”, false ); 40 container.add( italicButton ); 41 42 boldItalicButton = new JRadioButton( 43 “Bold/Italic”, false ); 44 container.add( boldItalicButton ); 45 46 // register events for JRadioButtons 47 RadioButtonHandler handler = new RadioButtonHandler(); 48 plainButton.addItemListener( handler ); 49 boldButton.addItemListener( handler ); 50 italicButton.addItemListener( handler ); 51 boldItalicButton.addItemListener( handler ); 52 53 // create logical relationship between JRadioButtons 54 radioGroup = new ButtonGroup(); 55 radioGroup.add( plainButton ); 56 radioGroup.add( boldButton ); 57 radioGroup.add( italicButton ); 58 radioGroup.add( boldItalicButton ); 59 60 // create font objects 61 plainFont = new Font( “Serif”, Font.PLAIN, 14 ); 62 boldFont = new Font( “Serif”, Font.BOLD, 14 ); 63 italicFont = new Font( “Serif”, Font.ITALIC, 14 ); 64 boldItalicFont = 65 new Font( “Serif”, Font.BOLD + Font.ITALIC, 14 ); 66 field.setFont( plainFont ); 67 68 setSize( 300, 100 ); 69 setVisible( true ); 70 } 71 72 // execute application 73 public static void main( String args[] ) 74 { 75 RadioButtonTest application = new RadioButtonTest(); 76 77 application.setDefaultCloseOperation( 78 JFrame.EXIT_ON_CLOSE ); 79 } 80 81 // private inner class to handle radio button events 82 private class RadioButtonHandler implements ItemListener { 83 84 // handle radio button events 85 public void itemStateChanged( ItemEvent event ) 86 { 87 // user clicked plainButton 88 if ( event.getSource() == plainButton ) 89 field.setFont( plainFont ); 90 Fig. 12.12 Fig. 12.12 Creating and manipulating radio button (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01