![]() ![]() ![]() ![]() |
Using Components, the GUI Building Blocks |
The Frameclass 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.
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 windowpublic 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(); } }Other interesting methods provided by Frame are:
String getTitle()
andvoid setTitle(String)
- Returns or sets (respectively) the title of the frame's window.
Image getIconImage()
andvoid setIconImage(Image)
- Returns or sets (respectively) the image displayed when the window is iconified.
MenuBar getMenuBar()
andvoid 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()
andvoid setResizable(boolean)
- Returns or sets whether the user can change the window's size.
int getCursorType()
andvoid 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.
![]() ![]() ![]() ![]() |
Using Components, the GUI Building Blocks |