Frames2 - Frame Events and Menus

In order for frames to handle events other than the default ones we need to extend the Frame class. In Java we write classes and then create an instance of the class that inherits all of the methods and properties of that class. We need to write a class that our applet will call to set up a frame with the methods needed to handle the events and actions. This class is known as a constructor class because it builds the object we want.

This next example illustrates how you can add menus to frames
and handle frame events.

The applet creates the first frame as soon as you start this applet in your browser. It can be resized, moved, maximized, or minimized. Click on File in the upper left corner to cause the drop-down menu to open. Select Quit to close the frame.

You can create multiple frames and close each one using the Menu-Quit option.

The diagram below illustrates the process of writing a constructor class to build the frame object that we want.

The frames2 class extends the Applet class and the demoframe class extends the Frame class. The frames2 applet calls the demoframe class to create a new frame. The demoframe class calls its 'super' class (Frame) to create the new frame so it inherits all the default properties and methods of the Frame class and the labels, menus and event handling methods of the demoframe class.

Notice that you can close the frame by using the menu - Quit item or by clicking on the close buttons. The Quit item creates an action and the close events cause an event called WINDOW_DESTROY to occur. Each must be handled by the demoframe class.

You can attach a MenuBar class to a frame to provide drop-down menus. To create a menu bar:

MenuBar topbar = new MenuBar();

Then to add it to a frame use the setMenuBar method. To add it from the applet:

myFrame.setMenuBar(topbar);

To add it from within the frame constructor:

setMenuBar(topbar);

Menus can then be added to the menu bar:

Menu fileMenu = new Menu("File");
topbar.add(fileMenu);
fileMenu.add("Quit");

The code for the Applet is listed below.

import java.applet.*;
import java.awt.*;

public class frames2 extends Applet

{
 Frame window = new demoframe("This is the 
   first frame.");
Import all the facilities of the AWT and applet that Java has to offer.

Create an applet called frames2.

Create an instance of the frame class, using the constuctor class demoframe.

  public void init()
  {
   window.show();
   add(new Label("Hit this button to"));
   add(new Button("Create another Frame"));
  }
The init method adds a label
The button created above is added to the applet.

Labels are added to explain the behavior of the frame.

The layout for the frame named window is set for FlowLayout. The default FlowLayout is center, top to bottom. Using a layout manager for frames is required.

Labels are added to the newly created frame.

public boolean action(Event evt, 
   Object whatAction)
  {
    if((evt.target instanceof Button))
     { 
	String buttonLabel = (String) 
            whatAction;
	if (buttonLabel == "Create another
            Frame")
	 {
          Frame window = new demoframe("This 
            is another frame.");
          window.show();
	 }
     }
    return false;
  }
}
When an action takes place this method tests for it.

If the action was an instance of Button, the string on the button is stored in buttonLabel

If the string on the button is "Destroy the Frame", hide the frame named window and dispose of it. Change the label on btn to "Create a new Frame" and return true.


If the string on the button is "Create a new Frame", resize to 300x200 and show the frame named window. Change the label on btn to "Destroy the Frame" and return true

otherwise return false.

The Following Class constructs the Frame

Notice the frames2 class is done. All the open braces are closed.
class demoframe extends Frame
{

demoframe(String title)
{
super(title); 
MenuBar topbar = new MenuBar();
setLayout(new FlowLayout());
setMenuBar(topbar);
Menu fileMenu = new Menu("File");
topbar.add(fileMenu);
fileMenu.add("Quit"); 
add(new Label("Use the File menu to close this 
    Window."));
resize(300,100);
} 
Here is where we create the new class
(extends Frame - the 'super' class)
We accept a string as a parameter
and call the Frame class passing it the title.
This way it inherits all of Frame's properties.
We create a new menu bar
set the layout of the frame
Place the menubar on the frame
Add the "File" item to the menu
Add a quit option to the menu.
Label the frame.
Set the size.
And our frame is initialized.
public boolean action(Event evt, Object
    whatAction)
{
if (evt.target instanceof MenuItem)
{
if ((String)whatAction == "Quit")
{
hide();
dispose();
return true;
}
}
return false;
} 
Our constructor class MUST handle all
actions and events for the frame!

If the menu is selected and the quit option
is invoked....
Then the frame is
hidden
and disposed.
Signal that the action was handled
or

If its not the action we want return false.
public boolean handleEvent(Event evt)
{
if (evt.id == Event.WINDOW_DESTROY)
{
hide();
dispose();
return true;
}
return super.handleEvent(evt);
} 
}
This handles the "close" event for the frame.
The WINDOW_DESTROY event occurs
when you click on the X at the top right of
the frame or you select Close in the control
icon at the top left of the frame.
In either case we close the frame
If the event is not the one we wanted we must
return the nature of the event to the Frame.
'super' class to handle the maximize, minimize
or resize events.
If we don't return this we will not only disable handling the events above but the action method will also not work.

 

If you would like to see the code for the menu applet click here and, if you like, copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "frames2.java". After compiling this program we need to create an HTML page with a reference to the class file. Save the HTML code in the same directory as the class file. When you load the HTML file in your browser the applet will execute.

Back to Frames