|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Handling Applet Menu EventsThe biggest problem with everything weve talked about so far is that we havent mentioned menu events or handled them. This section discusses how to handle menu events so that we can perform many tasks that our program requires when a user makes a menu selection. The first thing we need to do even before handling the menu events is handle the frame event when the user tries to close the frame. In the preceding chapter, the way we solved this problem was to create a new Frame class that extended the Frame class. In this new Frame class, we created a new class that extended the WindowAdapter class. Inside of the extended WindowAdapter class, we could then handle the window closing event. For more details, either review Day 5, Windows, or wait until we do the analysis of Listing 6.1. To handle the menu events, we first need to create a class that implements the ActionListener interface. Then, inside this new class, we add the actionPerformed() method. The actionPerformed() method is where all the menu events will come when a menu event has been triggered. The following example shows our new class that implements the ActionListener interface, and how it contains an actionPerformed() method that lets us handle the menu events: public class MyListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { if( ae.getSource() instanceof MenuComponent ) { // Handle your menu events here } } } Using a Menu in an AppletIve created an entire program that simply creates a Frame object and then creates a menu thats added to this frame. To keep things simple and recognizable, I use all the previous examples to build this program. See Listing 6.1. Listing 6.1 An Applet That Uses a Menu in a Frame Window 1 import java.awt.*; 2 import java.applet.*; 3 import java.awt.event.*; 4 5 public class Applet1 extends Applet 6 { 7 8 // Create the MyFrame object. 9 MyFrame m_Frame = new MyFrame( "Menu Tester" ); 10 11 Label m_lLabel = new Label( "No actions " ); 12 13 public void init() 14 { 15 m_Frame.add( m_lLabel ); 16 17 MyListener Listener = new MyListener(); 18 19 // Create the MenuBar object. 20 MenuBar menu = new MenuBar(); 21 22 // Create the file Menu object and add menu entries. 23 Menu file = new Menu( "File" ); 24 menu.add( file ); 25 file.add( "Open" ); 26 file.add( "Close" ); 27 file.addActionListener( Listener ); 28 29 // Create the options Menu object and add menu entries. 30 Menu options = new Menu( "Options" ); 31 menu.add( options ); 32 options.add( "Set to Red" ); 33 options.add( "Set to Green" ); 34 options.add( "Set to Blue" ); 35 options.addActionListener( Listener ); 36 37 // Create the actions Menu object and add menu entries. 38 Menu actions = new Menu( "Actions" ); 39 menu.add( actions ); 40 actions.add( "Go Home" ); 41 actions.add( "Go to Work" ); 42 actions.addActionListener( Listener ); 43 44 // Use the setMenuBar() method to tell the Frame 45 // class what MenuBar to use. 46 m_Frame.setMenuBar( menu ); 47 48 // Set the Frame size and make it visible. 49 m_Frame.setSize( 300, 200 ); 50 m_Frame.setVisible( true ); 51 52 } 53 54 public class MyListener implements ActionListener 55 { 56 public void actionPerformed( ActionEvent ae ) 57 { 58 if( ae.getSource() instanceof MenuComponent ) 59 { 60 m_lLabel.setText( "The '" + ae.getActionCommand() ⇒+ "' menu entry was selected." ); 61 } 62 } 63 } 64 65 public class MyFrame extends Frame 66 { 67 MyFrame( String s ) 68 { 69 super( s ); 70 71 // Add the extened WindowAdapter class. 72 addWindowListener( new WL() ); 73 } 74 75 // Our window listener class. 76 public class WL extends WindowAdapter 77 { 78 // Override the windowClosing() method. 79 public void windowClosing( WindowEvent e ) 80 { 81 // Hide the window. 82 setVisible( false ); 83 // Set the parent class to null. 84 m_Frame = null; 85 } 86 } 87 } 88 } When the program runs, the label will inform you that no action has taken place so far. Every time you make a menu selection, however, the label will reflect the menu selection you have made. You can see the program running in Figure 6.3.
Lets discuss this programs source code now. To make the explanations more clear, I dont always cover things in sequential order. At line 9, I create a frame window. The frame is not built from a straight Frame class; its built from my extended Frame class beginning at line 65. If you skip down to line 65, youll see a class declared called MyFrame that extends Frame. Notice that theres a single constructor that takes a string. This constructor first calls the super() and then calls the addWindowListener() method so that the frame window will get window messages. In the MyFrame class at line 76, youll see that we declare a class named WL that extends the WindowAdapter class. At line 79 we override the windowClosing() method to catch any window closing events. If you want to handle other window events, you then have to add other methods. Examples of other window events you might want to handle would be windowActivated() and windowOpened(). For more information on additional window events you can handle, refer to Day 5. Now look back up to line 17. This is where we create the listener. The listener is what well use to catch the menu events. Notice, though, that we dont use a standard ActionListener interface; we have to declare a class that implements the ActionListener interface. If youll look at line 54, you can see a very short class named MyListener that implements the ActionListener class. At line 56, youll see that weve overridden the actionPerformed() method. Its here that the program will come when a menu event is triggered. Now back to line 20. Here, we create our MenuBar object. In lines 2327, we create our File menu, add the File menu to the MenuBar object, add two menu entries (the Open and Close entries), and add the newly created Listener class to this Menu object. Lines 3035 do the same thing for the Options menu. The Options menu is created, its added to the MenuBar object, its menu entries are added, and then its told about the ActionListener. The last operations in the menu creation area (lines 3842) create and add the Actions menu. Youll see at line 46 that we use the Frame classs setMenuBar() method to add the MenuBar object to the frame window. Finally, at lines 49 and 50, we set the frame windows size and make it visible. After that, throughout the program for every menu event that is handled, at line 60 we set the label text to reflect the menu value of the menu item that has been selected.
|
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. |