Java Reference
In-Depth Information
Notice that all component classes begin with the letter
J
. For example, the class for a
label is
JLabel
, the class for a push button is
JButton
, and the class for a check box is
JCheckBox
. This chapter introduces five commonly used components:
JLabel
,
JButton
,
JTextField
,
JCheckBox
, and
JList
. Once you understand their basic operation, it will be
easy for you to learn to use the others.
Containers
Swing defines two types of containers. The first are top-level containers:
JFrame
,
JAp-
plet
,
JWindow
, and
JDialog
. These containers do not inherit
JComponent
. They do,
however, inherit the AWT classes
Component
and
Container
. Unlike Swing's other com-
ponents, which are lightweight, the top-level containers are heavyweight. This makes the
top-level containers a special case in the Swing component library.
As the name implies, a top-level container must be at the top of a containment hierarchy.
A top-level container is not contained within any other container. Furthermore, every con-
tainment hierarchy must begin with a top-level container. The one most commonly used for
applications is
JFrame
. The one used for applets is
JApplet
.
The second type of container supported by Swing is the lightweight container. Light-
weight containers
do
inherit
JComponent
. Examples of lightweight containers are
JPanel
,
JScrollPane
, and
JRootPane
. Lightweight containers are often used to collectively organ-
ize and manage groups of related components because a lightweight container can be con-