Thursday, 14 May 2015

Menus in Java

  You can put a menu bar in a frame or window, but not an applet. Refer to the illustration in Figure 17.3 showing the five menu-related classes: MenuComponent, MenuBar, Menu, MenuItem, and CheckboxMenuItem.
All the other menu classes inherit from MenuComponent. MenuComponent is an abstract class, but you'll use these methods fairly often.
Font, getFont()
Returns the font used for the current item.
setFont(Font a_font)
Sets the font to be used to display the item on which the menu is invoked.
The MenuBar class is a container for a set of menus displayed with a frame. The key MenuBar methods follow.
Menu, add(Menu a_menu)
Adds a menu to the menu bar. The return value is a handle to the added menu. Menus are added left to right. This is a synchronized method.
int, countMenus()
Returns the number of menus currently in the menu bar.
Menu, getHelpMenu()
Returns the menu that is defined as the Help menu for the menu bar.
Menu, getMenu(int pos)
Returns the menu item at a given location in the menu bar.
remove(int pos)
Removes the menu at the specified position. This is a synchronized method.
remove(MenuComponent menu)
Removes the specified menu from the menu bar. This is a synchronized method.
setHelpMenu(Menu a_menu)
Sets the specified menu to be the Help menu, which always is placed on the right side of the menu bar. This is a synchronized method.
The Menu class implements pull-down menus. There are two constructor methods and some useful Menu methods.
new Menu(String a_label)
Creates a new menu with the specified label.
new Menu(String a_label, boolean tear_off)
Creates a new menu with the specified label, which can be torn off from the menu bar.
MenuItem, add(MenuItem an_entry)
Adds the specified menu item to the menu. You make hierarchical menus by adding menus to another menu. This is a synchronized method.
add(String label)
Adds a new entry to the menu.
addSeparator()
Adds a separating line to the menu.
int, countItems()
Returns a count of the number of items in the menu.
MenuItem, getItem(int position)
Returns the menu item at the specified location.
boolean, isTearOff()
Returns TRUE if the menu has tear off enabled.
remove(int position)
Removes the menu item at the specified location. This is a synchronized method.
remove(MenuComponent an_item)
Removes the specified menu item. This is a synchronized method.
The MenuItem class implements the functionality of a single entry in a pull-down menu. When you create one, you have to supply its label (as a string) as the input parameter. You use the add method to add MenuItems to menus. The most useful MenuItem methods follow.
disable()
Grays out a menu item and prevents the user from selecting it.
enable()
Enables a menu item so the user can select it.
enable(boolean some_statement)
Enables a menu item if the logical statement evaluates to TRUE.
StringgetLabel()
Returns the item's label.
boolean, isEnabled()
Returns TRUE if the user can select the item.
setLabel(String new_label)
Changes the label of the menu item to the specified string.
An ACTION_EVENT is generated whenever a menu item is selected by the user.
The CheckboxMenuItem class extends MenuItem and implements the functionality of a menu item with an associated checkbox. The two methods of this class that will come in handy follow.
boolean, getState()
Returns TRUE if the menu item is checked.
setState(boolean new_state)
Sets the state of the menu item.

The applet in Listing 17.39 shows how to use menus. It creates a new frame to support the menu bar because you can't put a menu bar in an applet itself. The two buttons in the main applet window enable you to open and close the frame.Menus are a good example of how different your interfaces can look on different platforms. Mac users expect menu bars to show up at the top of the screen as the focus changes between windows. As a result, the peer classes on the Mac put the menu bar at the top of the screen rather than inside the frame, as a UNIX or Windows user would expect. Although the functionality is the same as you would see on any other platform, the details are designed to make Mac users feel comfortable. This is one of the strong points of Java: users on any platform will see an interface that looks familiar. Unfortunately, it does mean that you, the programmer, will have to spend a little extra time thinking about the implications of these types of differences on your user interface.

Listing 17.39. Using a menu bar.
import java.awt.*;
import java.applet.Applet;


public class menu_test extends Applet
{
    Frame f;
    Button b_open, b_close;
    boolean window_visible;

    public void init()
    {   Menu m_about, m_tools, m_sub;
        //these buttons will allow us to control the new window
        b_open = new Button("Open Window");
        b_close = new Button("Close Window");
        add(b_open);
        add(b_close); 
        //make a new frame because the applet can't have a menu bar
         f = new Frame("test");
        //set the initial size of the frame
        f.resize(200,200); 
        //make a new menu bar and attach it to the frame
        MenuBar mb = new MenuBar();
        f.setMenuBar(mb); 
        //set up the various menu items
        m_about = new Menu("About");
        m_about.add(new MenuItem("Credits"));
        m_about.add(new MenuItem("Register"));
        m_about.add(new MenuItem("Help"));
        m_tools = new Menu("Tools");
        m_tools.add(new MenuItem("Line"));
        m_tools.add(new MenuItem("Square"));
        //add a checkbox menu item
        m_tools.add(new CheckboxMenuItem("Dashed Lines"));
        //create a hierarchical menu
        m_sub = new Menu("Colors"); 
        m_sub.add("Red"); 
        m_sub.add("White"); 
        m_sub.add("Blue"); 
        //Make the menu hierarchical by adding it as though it were a menu
        //item
        m_tools.add(m_sub); 
        mb.setHelpMenu(m_about); 
        mb.add(m_tools); 
        mb.add(m_about); 
        //show the new frame
        f.show();
        window_visible = true;
    }
    public boolean action(Event the_event, Object the_arg) {
        String item_name; 
        //handle mouse clicks on the two buttons
        if(the_event.target instanceof Button) {
            item_name = (String)the_arg;
            if(item_name.equals("Open Window")) {
                if(!window_visible) {
                    f.show(); 
                    window_visible = true;
                } 
            } 
            if(item_name.equals("Close Window")) {
                if(window_visible) {
                    f.hide(); 
                    window_visible = false;
                } 
            } 
        }
        return true;
    }
}


No comments:

Post a Comment