Java Reference
In-Depth Information
The
Icon
Interface and Class
ImageIcon
Icons are a popular way to enhance the look-and-feel of an application and are also com-
monly used to indicate functionality. For example, the same icon is used to play most of
today's media on devices like DVD players and MP3 players. Several Swing components
can display images. An icon is normally specified with an
Icon
(package
javax.swing
) ar-
gument to a constructor or to the component's
setIcon
method. Class
ImageIcon
sup-
ports several image formats, including Graphics Interchange Format (GIF), Portable
Network Graphics (PNG) and Joint Photographic Experts Group (JPEG).
Line 28 declares an
ImageIcon
. The file
bug1.png
contains the image to load and store
in the
ImageIcon
object. This image is included in the directory for this example. The
ImageIcon
object is assigned to
Icon
reference
bug
.
Loading an Image Resource
In line 28, the expression
getClass().getResource("bug1.png")
invokes method
get-
Class
(inherited indirectly from class
Object
) to retrieve a reference to the
Class
object
that represents the
LabelFrame
class declaration. That reference is then used to invoke
Class
method
getResource
, which returns the location of the image as a URL. The
Im-
ageIcon
constructor uses the URL to locate the image, then loads it into memory. As we
discussed in Chapter 1, the JVM loads class declarations into memory, using a class loader.
The class loader knows where each class it loads is located on disk. Method
getResource
uses the
Class
object's class loader to determine the
location
of a resource, such as an image
file. In this example, the image file is stored in the same location as the
LabelFrame.class
file. The techniques described here enable an application to load image files from locations
that are relative to the class file's location.
Creating and Attaching
label2
Lines 29-30 use another
JLabel
constructor to create a
JLabel
that displays the text
"La-
bel
with
text
and
icon"
and the
Icon
bug
created in line 28. The last constructor argu-
ment indicates that the label's contents are left justified, or left aligned (i.e., the icon and
text are at the left side of the label's area on the screen). Interface
SwingConstants
(package
javax.swing
) declares a set of common integer constants (such as
SwingConstants.LEFT
,
SwingConstants.CENTER
and
SwingConstants.RIGHT
) that are used with many Swing
components. By default, the text appears to the right of the image when a label contains
both text and an image. The horizontal and vertical alignments of a
JLabel
can be set with
methods
setHorizontalAlignment
and
setVerticalAlignment
, respectively. Line 31
specifies the tool-tip text for
label2
, and line 32 adds
label2
to the
JFrame
.
Creating and Attaching
label3
Class
JLabel
provides methods to change a
JLabel
's appearance after it's been instantiated.
Line 34 creates an empty
JLabel
with the no-argument constructor. Line 35 uses
JLabel
method
setText
to set the text displayed on the label. Method
getText
can be used to
retrieve the
JLabel
's current text. Line 36 uses
JLabel
method
setIcon
to specify the
Icon
to display. Method
getIcon
can be used to retrieve the current
Icon
displayed on a label.
Lines 37-38 use
JLabel
methods
setHorizontalTextPosition
and
setVerticalTextPo-
sition
to specify the text position in the label. In this case, the text will be centered
horizon-
tally
and will appear at the
bottom
of the label. Thus, the
Icon
will appear
above
the text. The
horizontal-position constants in
SwingConstants
are
LEFT
,
CENTER
and
RIGHT
(Fig. 12.8).