Week15 - Chapter 25 Working with Frame Windows and other...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 25 Working with Frame Windows and other GUI Components After the applet, the type of window most often created is derived from Frame It is used to create child windows within applets Constructors: Frame( ) Frame(String title ) There are several methods that you will use when working with Frame windows: void resize(int newWidth , int newHeight ) void resize(Dimension newSize ) Dimension size( ) void hide( ) void show( ) Creates a standard window without title With title Dimension is an object in which the width is stored in width , and the height is stored in height You can obtain the current size of a window by calling size( ) . The size( ) method returns Dimension (the current size of the window contained within the width and height fields). Once a Frame window has been created, it will not be visible until you call show( ) . To hide a window (remove it from view), call hide( ) . newTitle is the new title for the window
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Responding the WINDOW_DESTROY Event All objects derived from Component receive events, such as mouse clicks and keypresses. This includes frame windows, which can override event methods such as mouseDown( ) (as described last week). There is one additional event that your frame windows need to handle --- the one generated when the window is closed. To do so, your applet must override its main event handler, handleEvent( ) . handleEvent( ) is the top-level event handler for your window. All events that relate to your window are routed through it. As we've seen, evtObj contains the Event object that describes the event. For mouse and keyboard events, handleEvent( ) simply passes evtObj along to the appropriate mouse or keyboard handler. This is why we did not need to use handleEvent( ) when creating applets last week. However, there are several other types of events that can only be handled by handleEvent( ) directly. One of these occurs when the user closes the window. When overriding handleEvent( ) , you must return true if you handle the event. Otherwise, you must pass the event along to the superclass implementation by calling super.handleEvent( ) . With handleEvent( ) , you can determine what event has occurred by examining the id field of the Event object passed to it as an argument. The Event class defines several constants that are the ID codes for the various events. Each time an event is generated, its ID is put in id . The only ID we need to use for the purposes of this chapter is WINDOW_DESTROY . Here is a skeleton of an overriden handleEvent( ) method that handles WINDOW_DESTROY . public boolean handleEvent(Event evtObj) { if (evtObj.id == Event.WINDOW_DESTROY) { // respond to event return true; } return super.handleEvent (evtObj); } Constructor: boolean handleEvent(Event evtObj)
Background image of page 2
How you respond to the WINDOW_DESTROY message depends upon whether the window is an application's main window or a child window of an applet: Applet: it must remove the window from the screen, using hide( ) . If you don't do this, then the window is never fully removed from the system.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

Week15 - Chapter 25 Working with Frame Windows and other...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online