Java Reference
In-Depth Information
Section 12.5 Displaying Text and Images in a Window
• Class
JFrame
provides the basic attributes and behaviors of a window.
•A
JLabel
(p. 481) displays read-only text, an image, or both text and an image. Text in a
JLabel
normally uses sentence-style capitalization.
• Each GUI component must be attached to a container, such as a window created with a
JFrame
(p. 483).
• Many IDEs provide GUI design tools (p. 529) in which you can specify the exact size and loca-
tion of a component by using the mouse; then the IDE will generate the GUI code for you.
•
JComponent
method
setToolTipText
(p. 483) specifies the tool tip that's displayed when the user
positions the mouse cursor over a lightweight component (p. 480).
•
Container
method
add
attaches a GUI component to a
Container
.
• Class
ImageIcon
(p. 484) supports several image formats, including GIF, PNG and JPEG.
•Method
getClass
of class
Object
(p. 484) retrieves a reference to the
Class
object that represents
the the class declaration for the object on which the method is called.
•
Class
method
getResource
(p. 484) returns the location of its argument as a URL. The method
getResource
uses the
Class
object's class loader to determine the location of the resource.
• The horizontal and vertical alignments of a
JLabel
can be set with methods
setHorizontal-
Alignment
(p. 484) and
setVerticalAlignment
(p. 484), respectively.
•
JLabel
methods
setText
(p. 484) and
getText
(p. 484) set and get the text displayed on a label.
•
JLabel
methods
setIcon
(p. 484) and
getIcon
(p. 484) set and get the
Icon
(p. 484) on a label.
•
JLabel
methods
setHorizontalTextPosition
(p. 484) and
setVerticalTextPosition
(p. 484)
specify the text position in the label.
•
JFrame
method
setDefaultCloseOperation
(p. 485) with constant
JFrame.EXIT_ON_CLOSE
as the
argument indicates that the program should terminate when the window is closed by the user.
•
Component
method
setSize
(p. 485) specifies the width and height of a component.
•
Component
method
setVisible
(p. 485) with the argument
true
displays a
JFrame
on the screen.
Section 12.6 Text Fields and an Introduction to Event Handling with Nested Classes
• GUIs are event driven—when the user interacts with a GUI component, events (p. 485) drive
the program to perform tasks.
• An event handler (p. 485) performs a task in response to an event.
• Class
JTextField
(p. 485) extends
JTextComponent
(p. 485) of package
javax.swing.text
,
which provides common text-based component features. Class
JPasswordField
(p. 485) extends
JTextField
and adds several methods that are specific to processing passwords.
•A
JPasswordField
(p. 485) shows that characters are being typed as the user enters them, but
hides the actual characters with echo characters (p. 485).
• A component receives the focus (p. 486) when the user clicks the component.
•
JTextComponent
method
setEditable
(p. 488) can be used to make a text field uneditable.
• To respond to an event for a particular GUI component, you must create a class that represents
the event handler and implements an appropriate event-listener interface (p. 488), then register
an object of the event-handling class as the event handler (p. 488).
•Non-
static
nested classes (p. 488) are called inner classes and are frequently used for event handling.
• An object of a non-
static
inner class (p. 488) must be created by an object of the top-level class
(p. 488) that contains the inner class.