Java Reference
In-Depth Information
(lines 28-31) that returns the preferred width and height of an
OvalPanel
and a
getMin-
imumSize
method (lines 34-37) that returns an
OvalPanel
's minimum width and height.
Methods
getPreferredSize
and
getMinimumSize
are used by some layout managers to
determine the size of a component.
Class
SliderFrame
(Fig. 22.3) creates the
JSlider
that controls the diameter of the
circle. Class
SliderFrame
's constructor (lines 17-45) creates
OvalPanel
object
myPanel
(line 21) and sets its background color (line 22). Lines 25-26 create
JSlider
object
diam-
eterJSlider
to control the diameter of the circle drawn on the
OvalPanel
. The
JSlider
constructor takes four arguments. The first specifies the orientation of
diameterJSlider
,
which is
HORIZONTAL
(a constant in interface
SwingConstants
). The second and third
arguments indicate the minimum and maximum integer values in the range of values for
this
JSlider
. The last argument indicates that the initial value of the
JSlider
(i.e., where
the thumb is displayed) should be
10
.
Lines 27-28 customize the appearance of the
JSlider
. Method
setMajorTick-
Spacing
indicates that each major tick mark represents 10 values in the range of values
supported by the
JSlider
. Method
setPaintTicks
with a
true
argument indicates that
the tick marks should be displayed (they aren't displayed by default). For other methods
that are used to customize a
JSlider
's appearance, see the
JSlider
online documentation
(
docs.oracle.com/javase/7/docs/api/javax/swing/JSlider.html
).
JSlider
s generate
ChangeEvent
s
(package
javax.swing.event
) in response to user
interactions. An object of a class that implements interface
ChangeListener
(package
javax.swing.event
) and declares method
stateChanged
can respond to
ChangeEvent
s.
Lines 31-41 register a
ChangeListener
to handle
diameterJSlider
's events. When
method
stateChanged
(lines 35-39) is called in response to a user interaction, line 38 calls
myPanel
's
setDiameter
method and passes the current value of the
JSlider
as an argu-
ment.
JSlider
method
getValue
returns the current thumb position.
A
JFrame
is a
window
with a
title bar
and a
border
. Class
JFrame
is a subclass of
Frame
(package
java.awt
), which is a subclass of
Window
(package
java.awt
). As such,
JFrame
is
one of the
heavyweight
Swing GUI components. When you display a window from a Java
program, the window is provided by the local platform's windowing toolkit, and therefore
the window will look like every other window displayed on that platform. When a Java
application executes on a Macintosh and displays a window, the window's title bar and
borders will look like those of other Macintosh applications. When a Java application ex-
ecutes on a Microsoft Windows system and displays a window, the window's title bar and
borders will look like those of other Microsoft Windows applications. And when a Java
application executes on a UNIX platform and displays a window, the window's title bar
and borders will look like those of other UNIX applications on that platform.
Returning Window Resources to the System
By default, when the user closes a
JFrame
window, it's hidden (i.e., removed from the
screen), but you can control this with
JFrame
method
setDefaultCloseOperation
. Inter-
face
WindowConstants
(package
javax.swing
), which class
JFrame
implements, declares
three constants—
DISPOSE_ON_CLOSE
,
DO_NOTHING_ON_CLOSE
and
HIDE_ON_CLOSE
(the de-
fault)—for use with this method. Some platforms allow only a limited number of windows