/* File JavaExamples/Caviness/CheckboxTest.java * * This software adapted from Horstmann & Cornell, "Core Java Volume 1- * Fundamentals," pp.384-386. * * 28 October 1997 (Caviness); 23 Nov 1998 (Erich Kaltofen EK) */ /** * This example application demonstrates the use of the JDK component * Checkbox. It displays a message and two check boxes that can be used * to toggle the font used in the message between bold and non-bold and * italic and non-italic. Both, one, or none can turned on. */ import java.awt.*; public class CheckboxTest extends Frame{ // EK: Hierarchy Component-->Container-->Window-->Frame-->CheckboxTest // contains 2 Panel's: font and radio // font contains two Checkbox's labeled Bold and Italic // radio contains a CheckboxGroup of four Checkboxes private FoxCanvas fox; private Checkbox bold; private Checkbox italic; final int small = 8; // Font sizes final int medium = 12; final int large = 14; final int extraLarge = 18; public CheckboxTest( String title ){ super( title ); // Set frame title; EK: Frame(String) Panel font = new Panel(); // A panel to hold the check boxes font.add( new Label( "Font Style" ) );// for font styles; EK: Container.add bold = addCheckbox( font,"Bold" ); // EK: CheckboxTest.addCheckbox italic = addCheckbox( font, "Italic" ); Panel radio = new Panel(); // Panel for radio buttons radio.add( new Label( "Font Size" ) ); // for font size CheckboxGroup fontSizeGrp = new CheckboxGroup(); Checkbox smallRB = addRadiobox( radio,"Small",fontSizeGrp,false ); // EK: CheckboxTest.addRadiobox(...) Checkbox medRB = addRadiobox( radio,"Medium",fontSizeGrp,true ); Checkbox largeRB = addRadiobox( radio,"Large",fontSizeGrp,false ); Checkbox exlargeRB = addRadiobox(radio,"ExtraLarge",fontSizeGrp,false); setLayout( new BorderLayout() ); // Layout frame; EK: Container.setLayout add( font,"North" ); // EK: Container.add(...) add( radio, "South" ); fox = new FoxCanvas(); add( fox,"Center" ); pack(); // EK: Frame.pack() } // end CheckboxTest(String) /* * Auxillary functions used by the constructor. */ public Checkbox addRadiobox( Panel p, String name, CheckboxGroup g, boolean on ){ Checkbox c = new Checkbox( name,g,on ); p.add( c ); // EK: Container.add(...) return c; } public Checkbox addCheckbox( Panel p, String name ){ Checkbox c = new Checkbox( name ); p.add( c ); return c; } public static void main( String arg[] ){ CheckboxTest f = new CheckboxTest( "Checkbox Test" ); f.show(); // Window.show() } /* * EK: handleEvent overrides Component.handleEvent(Event) */ public boolean handleEvent(Event evt){ if ( evt.target == this ) // catch Toolkit actions on Frame { System.out.println( "CheckboxTest.handleEvent() w evt = " + evt ); if ( evt.id == Event.WINDOW_DESTROY ) { this.dispose(); System.exit(0); } } return super.handleEvent(evt); // Component.handleEvent } // end handleEvent /* * action overrides Component.action(...). action is deprecated in * JDK 1.1 by the new event handling paradigm. */ public boolean action( Event e, Object obj ){ System.out.println( "CheckboxTest.action() w e = " + e + ", obj = " + obj ); if ( e.target instanceof Checkbox ){ Checkbox box = (Checkbox)e.target; String label = box.getLabel(); if ( label.equals("Bold") || label.equals("Italic") ){ int style = ( bold.getState() ? Font.BOLD : 0 ) + ( italic.getState() ? Font.ITALIC : 0 ); fox.setFontStyle( style ); return true; } if ( label.equals( "Small" ) ){ fox.setFontSize( small ); return true; } if ( label.equals( "Medium" ) ){ fox.setFontSize( medium ); return true; } if ( label.equals( "Large" ) ){ fox.setFontSize( large ); return true; } if ( label.equals( "ExtraLarge" ) ){ fox.setFontSize( extraLarge ); return true; } } return false; } // end CheckboxTest.action(...) } // end class CheckboxTest class FoxCanvas extends Canvas{ // EK: hierarchy Component-->Canvas-->FoxCanvas private int style; private int pointSize; public FoxCanvas(){ style = Font.PLAIN; // EK: FoxCanvas.style pointSize = 12; // EK: FoxCanvas.pointSize setFont(); // EK: FoxCanvas.setFont() setSize( 200, 50 ); // EK: Component.setSize(...) } public void setFont(){ setFont( new Font( "SansSerif",style,pointSize ) ); // EK: Component.setFont(Font) repaint(); // EK: Component.repaint() } public void setFontStyle( int style ){ this.style = style; setFont(); } public void setFontSize( int pointSize ){ this.pointSize = pointSize; setFont(); } public void paint( Graphics g ){ // overrides Component.paint(); executed by Component.update() // which is invoked by Component.repaint() g.drawString( "The quick brown fox jumps over the lazy dog.", 10, 25); } } // end class FoxCanvas