Previous | Next | Trail Map | Creating a User Interface | 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 can't currently use dialogs well, so they 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 uses a Frame. [Point to others?]

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 {
    private boolean inAnApplet = true;
    private 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.resize(250, 90);
        window.show();
    }
}


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