|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
When the program runs, youll see a menu with the three menu items. Each time you select a menu entry, the label in the Form window will show what you just selected, as shown in Figure 6.6.
At line 32, the event handler for the first menu items first menu entry can be seen. The menu event handlers go all the way through line 65; theres one event handler for each menu entry. Line 73 shows where the Main menu is created. Lines 7483 show where the menu items are created. As you can see in lines 88128, a lot of source code was added to create and populate the menu. Some methods that you might find very useful are available in applications. The first is the setChecked() method. This allows you to either check or uncheck a menu entry. The following example shows how to turn on a check for a menu item: menuItem1.setChecked( true ); Its also going to be important at times to gray out your menu entries. You do this by setting its enabled flag to false. The method to use is the setEnabled() method, which takes a single Boolean argument. The following example shows how to gray out a menu item by using the setEnabled() method with an argument of false: menuItem1.setEnabled( false ); Adding Pop-Up Menus in AppletsIts easy to add pop-up menus in your applets. The Popup class offers pretty much all you need in order to do this. You first need to create the Popup object. The following example shows how to do just that: Popup m_Popup = new Popup(); Then, you have to add the pop-up entries. You use the Popup classs add() method to do this. The following three lines of source code show how to add three menu entries to a pop-up menu: m_Popup.add( "Canvas to Red" ); m_Popup.add( "Canvas to Green" ); m_Popup.add( "Canvas to Blue" ); Finally, you have to add your pop-up to a component. In the following example, Ive created a button and then added a Popup object to it: Button button = new Button( "Test Popup" ); button.add( m_Popup ); Ive created a simple program, shown in Listing 6.3, that attaches a pop-up menu to a button. To handle the button event and show the pop-up menu when the button was pressed, I had to extend my Button class. This topic is covered in detail on Day 7, Applet User Interface Controls, so for now Ill just mention it without explaining it in-depth. Listing 6.3 This Program Shows How to Attach a Pop-Up Menu to a Button 1 import java.awt.*; 2 import java.applet.*; 3 import java.awt.event.MouseListener; 4 import java.awt.event.*; 5 6 public class Applet1 extends Applet 7 { 8 PopupMenu m_Popup = new PopupMenu(); 9 10 public void init() 11 { 12 m_Popup.add( "Canvas to Red" ); 13 m_Popup.add( "Canvas to Green" ); 14 m_Popup.add( "Canvas to Blue" ); 15 16 MyButton button = new MyButton( "Test Popup" ); 17 button.add( m_Popup ); 18 add( button ); 19 } 20 21 public class MyButton extends Button 22 { 23 MyButton( String s ) 24 { 25 super( s ); 26 enableEvents( AWTEvent.ACTION_EVENT_MASK ); 27 } 28 29 public void processActionEvent( ActionEvent ae ) 30 { 31 m_Popup.show( this, 0, 0 ); 32 } 33 } 34 35 } When the program runs, youll see a single button in the applet window. When you click the button, a pop-up menu with three choices will appear. SummaryAdding pull-down menus to your programs is a fundamental part of providing users the kind of user interface theyre accustomed to. Its not that hard with Visual J++. The classes that let you attach menus to Frame windows are straightforward and easy to use. And responding to menu events in Visual J++ applications is almost trivial.
|
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. |