Java Reference
In-Depth Information
Display 17.3
Some Methods in the Class JFrame
(part 2 of 2)
public
JFrame(String title)
Constructor that creates an object of the class
JFrame
with the title given as the argument.
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 defined 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
window listeners. When a window is
disposed
, it is eliminated but the program does not end.
To end the program, 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 in Chapter 20 on the website.)
If no action is specified using the method
setDefaultCloseOperation
, then the default
action taken is
JFrame.HIDE_ON_CLOSE
.
Throws an
IllegalArgumentException
if the argument is not one of the values listed above
3
.
Throws a
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
setSize(
int
width,
int
height)
Sets the size of the calling frame so that it has the
width
and
height
specified. Pixels are the
units of length used.
public void
setTitle(String title)
Sets the title for this frame to the argument string.
public void
add(Component componentAdded)
Adds a component to the
JFrame
.
public void
setLayout(LayoutManager manager)
Sets the layout manager.
Layout
managers are discussed later in this chapter in Section 17.3.
public void
setJMenuBar(JMenuBar menubar)
Sets the menu bar for the calling frame. (Menus and menu bars are discussed later in this chapter
in Section 17.4.)
public void
dispose()
Eliminates the calling frame and all its subcomponents. Any memory they use is released for
reuse. If there are items left (items other than the calling frame and its subcomponents), then this
does not end the program. (The method
dispose
is discussed in Chapter 19.)
3
If you have not yet covered Chapter 9 on exceptions, you can safely ignore all references to “throwing
exceptions.”