Frames

Java's Abstract Windowing Toolkit provides windows containers that allow you to create separate windows for your applications. When used with a Web browser, they can run outside the main window (unlike panels which are nested within the applet window.)

Frames have a title bar and if they are created by an applet, they should be destroyed BEFORE you close the applet that created them so that you can reclaim the resources they were using. This is how you create a frame with a title:

Frame window = new Frame("This is the Frames's Title Bar!");

There are several things you must be concerned with after a frame is created in order for it to be visible on the screen:

If you want to hide the frame again, in order to show a different frame for example, use the hide method.

window.hide();

Once a frame has been created and the show method has been called, it can be resized, maximized, and minimized just like any other window. When you are finished with the frame, you should always use the dispose method to get rid of it. Some of you system's resources are used when the frame is running and they will be restored when you get rid of it.

window.dispose();

 

This first example of a frame illustrates how you can create a frame from an applet that is running.

Notice that you can move, resize, maximize, and minimize it.

It can be moved outside the applet window that created it.

The only way you can dispose of this frame is to click on the "Destroy the Frame" button in the applet window. Be sure that you dispose of the frame before you the leave the page or close the applet. That way the resources the frame was using will be made available for other uses.

Notice that the frame can be minimized, resized or maximized but not closed by clicking on the X button or control icon on the top left and right of the frame. The applet's event handling routines cannot detect or deal with events that occur to the frame. This stems from the object oriented nature of Java. As you can see in the diagram below the Frame class will not handle many of the events that occur to the frame we've created.

 

The new frame inherits handlers for the Maximize, Minimize and Resize events from the Frame class but no others. Any other frame events or actions would need to be handled by a class that extends the Frame class. We will write this type of class in the next section.

The code for the Applet is listed below

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

public class frames extends Applet


{
 Frame window = new Frame("This is the Frame's
   Title Bar!");
 Button btn = new Button("Create a new Frame");
Import all the facilities of the AWT and applet that Java has to offer.

Create an applet called frames.

Create an instance of the frame class, initializing the title bar. Create an instance of the button class with a label.

 public void init()
  {
   add(new Label("Hit this button to"));
   add(btn);
   add(new Label("."));
   add(new Label("The new Frame is independent
      of the applet."));
   add(new Label("You can maximize and minimize it by 
      using"));
   add(new Label("the buttons on the top right or the 
      control icon."));
   add(new Label("on the top left. You will not be able 
      to close it."));
   add(new Label("You must use the applet's button to do
      that."));
   add(new Label("In order to handle Frame events you
      need to "));
   add(new Label("create a separate class for it."));
   window.setLayout(new FlowLayout());
   window.add(new Label("This is the Frame."));
   window.add(new Label("You can resize it, move it"));
   window.add(new Label("and perform other Windows
      operations."));
  }
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 == "Destroy the Frame")
{
window.hide();
window.dispose();
btn.setLabel("Create a new Frame");
return true;
}
if (buttonLabel == "Create a new Frame")
{
window.resize(300,200);
window.show();
btn.setLabel("Destroy the Frame");
return true;
}
}
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.

 

If you would like to see the code for the frames 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 "frames.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.

 


 

Adding Menus to Frames

Now that you have created frames, we can add a menu that allows us to close the frame from the frame itself instead of the applet that created it. You can run the applet "frames2" to see a demonstration of this.

Back to Java AWT Containers

Back to the Table of Contents