666 Graphical User Interface Components: Part 1 Chapter (Web host)
666 Graphical User Interface Components: Part 1 Chapter 12 27 field.setFont( new Font( “Serif”, Font.PLAIN, 14 ) ); 28 container.add( field ); 29 30 // create checkbox objects 31 bold = new JCheckBox( “Bold” ); 32 container.add( bold ); 33 34 italic = new JCheckBox( “Italic” ); 35 container.add( italic ); 36 37 // register listeners for JCheckBoxes 38 CheckBoxHandler handler = new CheckBoxHandler(); 39 bold.addItemListener( handler ); 40 italic.addItemListener( handler ); 41 42 setSize( 275, 100 ); 43 setVisible( true ); 44 } 45 46 // execute application 47 public static void main( String args[] ) 48 { 49 CheckBoxTest application = new CheckBoxTest(); 50 51 application.setDefaultCloseOperation( 52 JFrame.EXIT_ON_CLOSE ); 53 } 54 55 // private inner class for ItemListener event handling 56 private class CheckBoxHandler implements ItemListener { 57 private int valBold = Font.PLAIN; 58 private int valItalic = Font.PLAIN; 59 60 // respond to checkbox events 61 public void itemStateChanged( ItemEvent event ) 62 { 63 // process bold checkbox events 64 if ( event.getSource() == bold ) 65 66 if ( event.getStateChange() == ItemEvent.SELECTED ) 67 valBold = Font.BOLD; 68 else 69 valBold = Font.PLAIN; 70 71 // process italic checkbox events 72 if ( event.getSource() == italic ) 73 74 if ( event.getStateChange() == ItemEvent.SELECTED ) 75 valItalic = Font.ITALIC; 76 else 77 valItalic = Font.PLAIN; 78 Fig. 12.11 Program that creates two JCheckBoxbuttons (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01