Java Reference
In-Depth Information
public
JFrame()
Constructor that creates an object of the class
JFrame
.
public
JFrame(String title)
Constructor that creates an object of the class
JFrame
with the title given as the argument.
public
Component add(Component componentAdded)
Adds
componentAdded
to the
JFrame.
Typically used as a
void
method.
public
Container getContentPane()
Returns the content pane of the calling
JFrame
object.
Container
is a class in the package
java.awt
.
To set the color of a
JFrame
, use
getContentPane().setBackground(Color c)
If you use
setBackground
without the
getContentPane()
, you will not get any error
messages, but you will probably not see the color.
public void
setDefaultCloseOperation(
int
operation)
Sets the action that will happen by default when the user clicks the close-window button.
The argument should be one of the following defi ned constants:
JFrame.DO_NOTHING_ON_CLOSE:
Do nothing. The
JFrame
does nothing, but if there are any
registered window listeners, they are invoked. (Window listeners are explained in Chapter 18.)
JFrame.HIDE_ON_CLOSE:
Hide the frame after invoking any registered
WindowListener
objects.
JFrame.DISPOSE_ON_CLOSE:
Hide and
dispose
the frame after invoking any registered win-
dow listeners. When a window is
disposed
, it is eliminated but the program does not end. To
end the programs, use the next constant as an argument to
setDefaultCloseOperation
.
JFrame.EXIT_ON_CLOSE:
Exit the application using the
System exit
method. (Do not
use this for frames in applets. Applets are discussed on the accompanying CD.)
If no action is specifi ed using the method
setDefaultCloseOperation
, then the default
action taken is
JFrame.HIDE_ON_CLOSE.
Throws:
IllegalArgumentException
if the argument is not one of the values listed previously.
SecurityException
if the argument is
JFrame.EXIT_ON_CLOSE
and the Security Manager
will not allow the caller to invoke
System.exit
. (You are not likely to encounter this case.)
public void
setLayout(LayoutManager manager)
Makes
manager
the layout manager for the
JFrame
.
public void
setSize(
int
width,
int
height)
Sets the size of the calling frame so that it has the
width
and
height
specifi ed. Pixels are the
units of length used.