There are several key methods you will use when working with Frame windows. They
are examined here.
Setting the Window's Dimensions
The setSize( ) method is used to set the dimensions of the window. Its signature is shown here:
void setSize(int newWidth, int newHeight)
void setSize(Dimension newSize)
The new size of the window is specified by newWidth and newHeight, or by the width and
height fields of the Dimension object passed in newSize. The dimensions are specified in
terms of pixels.
The getSize( ) 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 the width and height
fields of a Dimension object.
Hiding and Showing a Window
After a frame window has been created, it will not be visible until you call setVisible( ).
Its signature is shown here:
void setVisible(boolean visibleFlag)
The component is visible if the argument to this method is true. Otherwise, it is hidden.
Setting a Window's Title
You can change the title in a frame window using setTitle( ), which has this general form:
void setTitle(String newTitle)
Here, newTitle is 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 calling setVisible(false). To intercept a window-close event, you must implement
the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ),
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 of Frame, 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 of Frame. Doing so lets you override Frame's methods
and provide event handling.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks