• In Java terminology, a frame is a window with a title and a border.
  • A frame may also have a menu bar.
  • Frames play an important role in the AWT because a GUI program normally displays a frame when it's executed.
  • The DrawableFrame objects used in previous chapters are examples of frames.
  • Frames are created using one of the constructors in the Frame class.
  • One constructor takes a single argument (the title to be displayed at the top of the frame):
  • Frame f = new Frame("Title goes here");
  • Although the Frame object now exists, it's not visible on the screen.
  • Before making the frame visible, a method should be called to set the size of the frame.
  • If desired, the frame's location can also be specified.

Frame Methods

  • Many methods used with Frame objects are inherited from Window (Frame's superclass) or from Component (Window's superclass).
  • The setSize method sets the width and height of a frame:
  • f.setSize(width, height);
  • If a program fails to call setSize or pack before displaying a frame, it will assume a default size.
  • The size of a frame can change during the execution of a program.
  • The getSize method returns a frame's current width and height:
  • Dimension frameSize = f.getSize();
  • frameSize.width will contain f's width. frameSize.height will contain f's height.
  • The setVisible method controls whether or not a frame is currently visible on the screen.
  • Calling setVisible with true as the argument makes a frame visible:
  • f.setVisible(true);
  • Calling it with false as the argument makes the frame disappear from the screen:
  • f.setVisible(false);
  • The Frame object still exists; it can be made to reappear later by calling setVisible again.

Creating a Frame

  • The FrameTest program creates a Frame object and displays it on the screen.
  • This program illustrates three key steps:
    1. Using the Frame constructor to create a frame.
    2. Setting the size of the frame.
    3. Displaying the frame on the screen.
// Displays a frame on the screen.
// WARNING: Frame cannot be closed.
import java.awt.*;
public class FrameTest 
	public static void main(String[] args) 
		Frame f = new Frame("Frame Test");

Output :

AWT Frame Test

Note: As with the other AWT components, the appearance of a frame depends on the platform.

Setting the Location of a Frame

  • By default, all windows (including frames) are displayed in the upper-left corner of the screen, which has coordinates (0, 0).
  • The setLocation method can be used to specify a different location:
  • f.setLocation(50, 75);
  • To find the current location of a frame, call getLocation:
  • Point frameLocation = f.getLocation();
  • The coordinates of f's upper-left corner will be stored in frameLocation.x and frameLocation.y.

Adding Components to a Frame

  • The Frame class is rarely used to create objects directly.
  • Instead, it's customary to define a subclass of Frame and then create an instance of the subclass.
  • This strategy makes it possible to tailor the subclass.
  • In particular, the constructor for the subclass can put components into the frame.