![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Sample ProgramsIve created two sample programs to show how to add check boxes to an applet, and how to handle the events. The first program extends the Checkbox class and lets the extended class handle the events. The second program adds an ItemListener and lets a new class (which implements the ItemListener interface) handle the events.
Listing 7.3 The First CheckboxDemo Program Shows the Extended Checkbox Class 1 import java.awt.*; 2 import java.applet.*; 3 import java.awt.event.*; 4 5 public class Applet1 extends Applet 6 { 7 String m_strDisplay = "No events"; 8 9 Checkbox m_Checkbox1 = new Checkbox( "One" ); 10 Checkbox m_Checkbox2 = new Checkbox( "Two" ); 11 Checkbox m_Checkbox3 = new Checkbox( "Three" ); 12 MyCheckbox m_Checkbox4 = new MyCheckbox( "Four" ); 13 MyCheckbox m_Checkbox5 = new MyCheckbox( "Five" ); 14 MyCheckbox m_Checkbox6 = new MyCheckbox( "Six" ); 15 16 public void init() 17 { 18 add( m_Checkbox1 ); 19 add( m_Checkbox2 ); 20 add( m_Checkbox3 ); 21 add( m_Checkbox4 ); 22 add( m_Checkbox5 ); 23 add( m_Checkbox6 ); 24 } 25 26 public void paint( Graphics g ) 27 { 28 g.drawString( m_strDisplay, 20, 100 ); 29 } 30 31 public class MyCheckbox extends Checkbox 32 { 33 String m_strText; 34 35 MyCheckbox( String s ) 36 { 37 super( s ); 38 m_strText = s; 39 enableEvents( AWTEvent.ITEM_EVENT_MASK ); 40 } 41 42 public void processItemEvent( ItemEvent ie ) 43 { 44 m_strDisplay = "'" + m_strText + "' was just pressed. "; 45 getParent().repaint(); 46 super.processItemEvent( ie ); 47 } 48 49 } 50 51 }
Listing 7.4 The Second CheckboxDemo Program Uses an ItemListener to Handle Checkbox Events 1 import java.awt.*; 2 import java.applet.*; 3 import java.awt.event.*; 4 5 public class Applet1 extends Applet 6 { 7 String m_strDisplay = "No events"; 8 9 Checkbox m_Checkbox1 = new Checkbox( "One" ); 10 Checkbox m_Checkbox2 = new Checkbox( "Two" ); 11 Checkbox m_Checkbox3 = new Checkbox( "Three" ); 12 Checkbox m_Checkbox4 = new Checkbox( "Four" ); 13 Checkbox m_Checkbox5 = new Checkbox( "Five" ); 14 Checkbox m_Checkbox6 = new Checkbox( "Six" ); 15 16 MyListener Listener = new MyListener(); 17 18 public void init() 19 { 20 m_Checkbox4.addItemListener( Listener ); 21 m_Checkbox5.addItemListener( Listener ); 22 m_Checkbox6.addItemListener( Listener ); 23 add( m_Checkbox1 ); 24 add( m_Checkbox2 ); 25 add( m_Checkbox3 ); 26 add( m_Checkbox4 ); 27 add( m_Checkbox5 ); 28 add( m_Checkbox6 ); 29 } 30 31 public void paint( Graphics g ) 32 { 33 g.drawString( m_strDisplay, 20, 100 ); 34 } 35 36 public class MyListener implements ItemListener 37 { 38 public void itemStateChanged( ItemEvent ie ) 39 { 40 if( ie.getSource() == m_Checkbox4 ) 41 { 42 m_strDisplay = "'Four' was pressed."; 43 repaint(); 44 } 45 else if( ie.getSource() == m_Checkbox5 ) 46 { 47 m_strDisplay = "'Five' was pressed."; 48 repaint(); 49 } 50 else if( ie.getSource() == m_Checkbox6 ) 51 { 52 m_strDisplay = "'Six' was pressed."; 53 repaint(); 54 } 55 } 56 } 57 58 }
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |