Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
There are several key methods you will use when working withFramewindows. They
are examined here.

Setting the Window’s Dimensions

ThesetSize( )method is used to set the dimensions of the window. Its signature is shown here:

void setSize(intnewWidth, intnewHeight)
void setSize(DimensionnewSize)

The new size of the window is specified bynewWidthandnewHeight, or by thewidthand
heightfields of theDimensionobject passed innewSize. The dimensions are specified in
terms of pixels.
ThegetSize( )method is used to obtain the current size of a window. Its signature is
shown here:

Dimension getSize( )

This method returns the current size of the window contained within thewidthandheight
fields of aDimensionobject.

Hiding and Showing a Window

After a frame window has been created, it will not be visible until you callsetVisible( ).
Its signature is shown here:

void setVisible(booleanvisibleFlag)

The component is visible if the argument to this method istrue. Otherwise, it is hidden.

Setting a Window’s Title

You can change the title in a frame window usingsetTitle( ), which has this general form:

void setTitle(StringnewTitle)

Here,newTitleis the new title for the window.

Closing a Frame Window

When using a frame window, your program must remove that window from the screen when
it is closed, by callingsetVisible(false). To intercept a window-close event, you must implement
thewindowClosing( )method of theWindowListenerinterface. InsidewindowClosing( ),
you must remove the window from the screen. The example in the next section illustrates
this technique.

Creating a Frame Window in an Applet
While it is possible to simply create a window by creating an instance ofFrame, you will
seldom do so, because you will not be able to do much with it. For example, you will not be
able to receive or process events that occur within it or easily output information to it. Most
of the time, you will create a subclass ofFrame. Doing so lets you overrideFrame’s methods
and provide event handling.

668 Part II: The Java Library

Free download pdf