Java Reference
In-Depth Information
The program's output is a bit silly—it's just a tiny window:
In fact, there is another problem with the program: Closing the window doesn't
actually terminate the Java program. When you display a JFrame on the screen, by
default Java does not exit the program when the frame is closed. You can tell that the
program hasn't exited because a console window will remain on your screen (if
you're using certain Java editors) or because your editor does not show its usual mes-
sage that the program has terminated. If you want the program to exit when the win-
dow closes, you have to say so explicitly.
To create a more interesting frame, you'll have to modify some of the properties of
JFrame s. A property of a GUI component is a field or attribute it possesses internally
that you may wish to examine or change. A frame's properties control features like the
size of the window or the text that appears in the title bar. You can set or examine these
properties' values by calling methods on the frame.
Table 14.3 lists several useful JFrame properties. For example, to set the title text
of the frame in the SimpleFrame program to “A window frame”, you'd write the fol-
lowing line of code:
frame.setTitle("A window frame");
Table 14.3
Useful Properties That Are Specific to JFrames
Property
Type
Description
Methods
default Close
What should happen when the
int
getDefaultCloseOperation,
operation
frame is closed; choices include:
setDefaultCloseOperation(int)
JFrame.DO_NOTHING_ON_
CLOSE (don't do anything)
JFrame.HIDE_ON_CLOSE
(hide the frame)
JFrame.DISPOSE_ON_CLOSE
(hide and destroy the frame so
that it cannot be shown again)
JFrame.EXIT_ON_CLOSE
(exit the program)
icon image
The icon that appears in the
Image
getIconImage,
title bar and Start menu or Dock
setIconImage(Image)
layout
An object that controls the
LayoutManager
getLayout,
positions and sizes of the
setLayout(LayoutManager)
components inside this frame
resizable
Whether or not the frame allows
boolean
isResizable,
itself to be resized
setResizable(boolean)
title
The text that appears in the
String
getTitle, setTitle(String)
frame's title bar
 
Search WWH ::




Custom Search