Frames
- In Java terminology, a frame is a window with a
titleand aborder. - 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
DrawableFrameobjects used in previous chapters are examples of frames. - Frames are created using one of the
constructorsin 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");
not visible on
the screen.set the size of the frame.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);
Dimension frameSize = f.getSize();
f.setVisible(true);
f.setVisible(false);
Creating a Frame
- The FrameTest program creates a Frame object and displays it on the screen.
- This program illustrates three key steps:
- Using the Frame constructor to create a frame.
- Setting the size of the frame.
- 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");
f.setSize(214,141);
f.setVisible(true);
}
}
Output :
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);
Point frameLocation = f.getLocation();
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.


