Previous | Next | Trail Map | Creating a User Interface (with Swing) | Using Components, the GUI Building Blocks

How to Use Frames

The Frame(in the API reference documentation) class provides windows for applets and applications. Every application needs at least one Frame. If an application has a window that should be dependent on another window -- disappearing when the other window is iconified, for example -- then you should use a Dialog instead of a Frame for the dependent window. Unfortunately, applets currently can't use dialogs well, so they generally need to use frames instead.

The How to Use Menus page and the How to Use Dialogs page are two of the many in this tutorial that use a Frame.

Below is the code that the menu demonstration uses to create its window (a Frame subclass) and handle the case where the user closes the window.

public class MenuWindow extends Frame {
    boolean inAnApplet = true;
    TextArea output;

    public MenuWindow() {
        ...//This constructor implicitly calls the Frame no-argument 
        //constructor and then adds components to the window.
    }

    public boolean handleEvent(Event event) {
        if (event.id == Event.WINDOW_DESTROY) {
            if (inAnApplet) {
                dispose();
            } else {
                System.exit(0);
            }
        }   
        return super.handleEvent(event);
    }

. . .

    public static void main(String args[]) {
        MenuWindow window = new MenuWindow();
        window.inAnApplet = false;

        window.setTitle("MenuWindow Application");
        window.pack();
        window.show();
    }
}

The pack() method, which is called in the main() method above, is defined by the Window class. See How to Use Dialogs for information about pack().

Besides the no-argument Frame constructor implicitly used by the MenuWindow constructor shown above, the Frame class also provides a one-argument constructor. The argument is a String that specifies the title of the frame's window

Other interesting methods provided by Frame are:

String getTitle() and void setTitle(String)
Returns or sets (respectively) the title of the frame's window.
Image getIconImage() and void setIconImage(Image)
Returns or sets (respectively) the image displayed when the window is iconified.
MenuBar getMenuBar() and void setMenuBar(MenuBar)
Returns or sets (respectively) the menu bar for this Frame.
void remove(MenuComponent)
Removes the specified menu bar from this Frame.
boolean isResizable() and void setResizable(boolean)
Returns or sets whether the user can change the window's size.
int getCursorType() and void setCursor(int)
Gets the current cursor image or sets the cursor image. The cursor must be specified as one of the types defined in the Frame class. The pre-defined types are Frame.DEFAULT_CURSOR, Frame.CROSSHAIR_CURSOR, Frame.HAND_CURSOR, Frame.MOVE_CURSOR, Frame.TEXT_CURSOR, Frame.WAIT_CURSOR, or Frame.X_RESIZE_CURSOR, where X is SW, SE, NW, NE, N, S, W, or E.


Previous | Next | Trail Map | Creating a User Interface (with Swing) | Using Components, the GUI Building Blocks