![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
At line 36, the processActionEvent() method begins. Its this method that handles the events. The first thing that happens in the method is that it sets the display string so that the next time the applet window is repainted, youll see which button was pressed. The next thing it does is to cause its parent, which is the Main applet window, to repaint. The last thing it does is call the super.processActionEvent() method. Now, notice back at lines 9, 10, and 11 that instead of creating normal button objects, were creating MyButton objects. We do this because we need to use the extended Button class to handle the events. Theres another way of handling the Button events, as mentioned earlier. Ive written a second program, shown in Listing 7.2, that has an identical appearance to the first program but implements the Button event handling differently. Why would you need two different ways to handle Button events? Well, it depends on the program youre writing and whats easier for you. In certain circumstances one way will be easier, and in other circumstances the other way might be more to your liking.
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 buttons have been pressed."; 8 9 Button m_Button1 = new Button( "First Button" ); 10 Button m_Button2 = new Button( "Second Button" ); 11 Button m_Button3 = new Button( "Third Button" ); 12 13 MyListener Listener = new MyListener(); 14 15 public void init() 16 { 17 m_Button1.addActionListener( Listener ); 18 m_Button2.addActionListener( Listener ); 19 m_Button3.addActionListener( Listener ); 20 add( m_Button1 ); 21 add( m_Button2 ); 22 add( m_Button3 ); 23 } 24 25 public void paint( Graphics g ) 26 { 27 g.drawString( m_strDisplay, 20, 120 ); 28 } 29 30 public class MyListener implements ActionListener 31 { 32 public void actionPerformed( ActionEvent ae ) 33 { 34 if( ae.getSource() == m_Button1 ) 35 { 36 m_strDisplay = "The 'First Button' was pressed."; 37 repaint(); 38 } 39 else if( ae.getSource() == m_Button2 ) 40 { 41 m_strDisplay = "The 'Second Button' was pressed."; 42 repaint(); 43 } 44 else if( ae.getSource() == m_Button3 ) 45 { 46 m_strDisplay = "The 'Third Button' was pressed."; 47 repaint(); 48 } 49 } 50 } 51 52 }
The Checkbox ControlThe Checkbox user interface component has two states: true (or selected) and false (or deselected). Checkbox components trigger user events when selected and deselected. When more than one Checkbox appears within a container, they can either be independently selected or be exclusive of each other. For Checkbox controls to be exclusive of each other, they must all belong to the same group. Only one Checkbox component in a group can be selected at a time. The Checkbox components in a group are usually referred to as radio buttons. In Java, you can give Checkbox components the behavior of radio buttons by associating each component with another object by Checkbox group. Well talk about the Checkbox group later in the next section.
|
![]() |
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. |