We have learned how to handle events in JDK 1.1 and we have learned how to use the layout managers in JDK 1.1. These two topics form the basis for the design and implementation of a Graphical User Interface.
The next step is to take a look at the variety of components that are available to combine with layout and event handling to produce an effective Graphical User Interface.
The available components are defined by classes in the package java.awt. Our approach will be to group those classes into categories and study the material on a category basis. As of this writing, it looks as if the remaining categories are:
java.lang.Object | +----MenuShortcut | +----java.awt.MenuComponent | +----java.awt.MenuBar | +----java.awt.MenuItem | +----java.awt.Menu | +----java.awt.CheckboxMenuItem | +----java.awt.PopupMenu |
The first thing that we need to do is to take a very brief look at all the classes listed in the above hierarchy to get an overview of what lies ahead.
Before we look seriously at the four classes listed above, let's take a brief look at the MenuComponent class.
Although the JDK 1.1.3 documentation shows the constructor as
public MenuComponent() |
junk.java:97: class java.awt.MenuComponent is an abstract class.
It can't be instantiated.
MenuComponent myMenu = new MenuComponent(); |
The third version has a boolean parameter referred to as tearOff
in the detailed documentation. The following description from the JDK 1.1
documentation describes the use of the boolean parameter in the third version
of the constructor.
"Constructs a new Menu with the specified label. If tearOff is true, the menu can be torn off - the menu will still appear on screen after the the mouse button has been released. NOTE: tear-off functionality may not be supported by all AWT implementations. If a particular implementation doesn't support tear-offs, this value will be silently ignored." |
There are more than a dozen methods provided by this class, and of course objects of this class also have access to the methods of the MenuComponent class and its superclasses. Some of the particularly interesting methods in this class are listed below:
This class provides about 20 methods, with some of the more interesting ones listed below:
" A class that encapsulates the platform's concept of a menu bar bound to a Frame. In order to associate the MenuBar with an actual Frame, the Frame.setMenuBar() method should be called." |
According to the JDK 1.1 documentation, the first parameter shown as
as int is:
"the raw keycode for this MenuShortcut, as would be returned in the keyCode field of a KeyEvent if this key were pressed" |
A small sampling of those symbolic constants, taken from the JDK 1.1
documentation, is shown below.
|
This application places two menus in a Frame object. One of the menus has two items and the other has three items.
The first item in the first menu also has a shortcut which is Ctrl+Shift+K.
When a menu item is selected, this generates an ActionEvent which in turn causes a line of text to be displayed on the screen identifying the item that was selected.
Typical screen outputs when menu items are selected using the mouse
or the shortcut key are:
java.awt.MenuItem[menuitem4,label=Third Item on Menu B]
java.awt.MenuItem[menuitem0,label=First Item on Menu A,shortcut=Ctrl+Shift+K] |
The program was tested using JDK 1.1.3 running under Win95.
Also, the program generates two separate menus and essentially the same code is required for each. Therefore, in this section, we will be highlighting typical code fragments of the type required to perform each of the necessary operations.
Probably the most interesting thing about these code fragments is not the code itself, but rather the order in which the different operations are performed to accomplish the separate steps necessary to construct the menu. You should pay particular attention to the steps involved.
The first interesting code fragment instantiates a MenuShortcut object that will later be used in the instantiation of a MenuItem object.
Several such MenuItem objects will be added to a Menu object to produce a menu with several choices.
Two such Menu objects will be added to a MenuBar object to produce a menu bar with two separate menus.
The MenuBar object will then be associated with the Frame object.
Now that you have the overview, let's go back to the MenuShortcut object.
The argument list for the constructor in this case specifies a shortcut key combination consisting of Ctrl+Shift+K.
The true boolean parameter specifies the requirement for the Shift key.
The Ctrl key is apparently always required. The first parameter
to the constructor is a symbolic constant defined in the KeyEvent class
which specifies the "K" key.
MenuShortcut myShortcut = new MenuShortcut(KeyEvent.VK_K,true); |
The next interesting code fragment is typical of that required to instantiate
MenuItem objects which will later be added to the Menu object
to create the actual menu. Two different styles of instantiation are shown.
The first style specifies a shortcut for the item as discussed above. The
second style does not specify a shortcut.
MenuItem firstItemOnMenuA = new MenuItem("First Item on Menu A",myShortcut); MenuItem secondItemOnMenuA = new MenuItem("Second Item on Menu A"); |
MyMenuProcessor myMenuProcessor = new MyMenuProcessor(); firstItemOnMenuA.addActionListener(myMenuProcessor); secondItemOnMenuA.addActionListener(myMenuProcessor); |
Menu menuA = new Menu("Menu A"); menuA.add(firstItemOnMenuA); |
MenuBar menuBar = new MenuBar(); menuBar.add(menuA); menuBar.add(menuB); |
public synchronized void setMenuBar(MenuBar mb)
Sets the menu bar for this frame to the specified menu bar. Parameters: mb - the menu bar being set |
Frame myFrame = new Frame("Copyright 1997, R.G.Baldwin"); myFrame.setMenuBar(menuBar); |
/*File Menu01.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1 This application places two menus in a Frame object. One of the menus has two items and the other has three items. The first item in the first menu also has a shortcut which is Ctrl+Shift+K. When a menu item is selected, this generates an Action event which in turn causes a line of text to be displayed on the screen identifying the item that was selected. Typical screen outputs when menu items are selected using the Mouse are (line breaks manually inserted for this document). java.awt.MenuItem[menuitem4,label=Third Item on Menu B] java.awt.MenuItem[menuitem0,label=First Item on Menu A, shortcut=Ctrl+Shift+K] A windowClosing() event listener object is instantiated and registered on the frame to terminate the program when the frame is closed. The program was tested using JDK 1.1.3 running under Win95. **********************************************************/ import java.awt.*; import java.awt.event.*; //=======================================================// public class Menu01 { public static void main(String[] args){ GUI gui = new GUI();//instantiate a GUI }//end main }//end class Menu01 //=======================================================// class GUI { public GUI(){//constructor //Instantiate a MenuShortcut object MenuShortcut myShortcut = new MenuShortcut(KeyEvent.VK_K,true); //Instantiate several MenuItem objects MenuItem firstItemOnMenuA = new MenuItem("First Item on Menu A",myShortcut); MenuItem secondItemOnMenuA = new MenuItem("Second Item on Menu A"); MenuItem firstItemOnMenuB = new MenuItem("First Item on Menu B"); MenuItem secondItemOnMenuB = new MenuItem("Second Item on Menu B"); MenuItem thirdItemOnMenuB = new MenuItem("Third Item on Menu B"); //Instantiate an ActionListener object and register // it on the MenuItem objects. MyMenuProcessor myMenuProcessor = new MyMenuProcessor(); firstItemOnMenuA.addActionListener(myMenuProcessor); secondItemOnMenuA.addActionListener(myMenuProcessor); firstItemOnMenuB.addActionListener(myMenuProcessor); secondItemOnMenuB.addActionListener(myMenuProcessor); thirdItemOnMenuB.addActionListener(myMenuProcessor); //Instantiate two Menu objects and add the MenuItem // objects to them Menu menuA = new Menu("Menu A"); menuA.add(firstItemOnMenuA); menuA.add(secondItemOnMenuA); Menu menuB = new Menu("Menu B"); menuB.add(firstItemOnMenuB); menuB.add(secondItemOnMenuB); menuB.add(thirdItemOnMenuB); //Instantiate a MenuBar object and add the Menu // objects to it MenuBar menuBar = new MenuBar(); menuBar.add(menuA); menuBar.add(menuB); //Instantiate a Frame object and associate the MenuBar // object with the Frame object. Note that this is // NOT a typical myFrame.add() method invocation, but // rather is a special form of method invocation that // is required to associate a MenuBar object with a // Frame object. Frame myFrame = new Frame("Copyright 1997, R.G.Baldwin"); //note that this is not an add method myFrame.setMenuBar(menuBar); myFrame.setSize(250,100); myFrame.setVisible(true); //Instantiate and register a window listener to // terminate the program when the Frame is closed. myFrame.addWindowListener(new Terminate()); }//end constructor }//end class GUI definition //=======================================================// //Class to instantiate an ActionListener object to be // registered on the menu items. class MyMenuProcessor implements ActionListener{ public void actionPerformed(ActionEvent e){ //Display the menu item that generated the ActionEvent System.out.println(e.getSource()); }//end actionPerformed }//end class MyMenuProcessor //=======================================================// class Terminate extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program }//end windowClosing }//end class Terminate //=======================================================// |