Java Reference
In-Depth Information
graphics that need to be updated relatively frequently — such as when you create an animation. When part
of what is displayed is to be animated, a static background can be displayed independently via the
con-
tentPane
. Because this doesn't need to be reprocessed each time the animated objects need to be redrawn,
the whole process can be much more efficient.
The
JFrame
class defines methods to provide you with a reference to any of the panes (shown in
Table
TABLE 17-3
:
JFrame Class Methods
METHOD DESCRIPTION
getRootPane()
Returns the root pane as type
JRootPane
.
getLayeredPane()
Returns the layered pane as type
JLayeredPane
.
getContentPane()
Returns the content pane as type
Container
. This is the method you use most frequently because
you normally add components to the content pane.
getGlassPane()
Returns the glass pane as type
Component.
All the classes here that represent panes are themselves Swing components, defined in the
javax.swing
package. A
JApplet
object has the same arrangement of panes as a
JFrame
object, so adding components to
an applet, or drawing on it, works in exactly the same way. An applet defined as a
JApplet
object can also
have a menu bar just like an application window.
All the panes, as well as the menu bar, are components, so before I start delving into how to add a menu
bar or other components to a window, let's unearth a little more about the makeup of components in general.
BASICS OF COMPONENTS
You need to understand several basic concepts common to all components before you can apply them prop-
erly. They also have uses in many different contexts. Although this section may seem like something of a
catalog of classes and methods without much in the way of practical application, please stay with it. You use
most of these capabilities later. To understand the fundamental things you can do with Swing components,
you need to explore the functionality the Swing components inherit from the
Component
and
Container
classes.
When a component is contained within another component, the outer object is referred to as the
parent
.
You can obtain a reference to the parent object of any given component by calling its
getParent()
method.
This method is inherited from the
Component
class, and it returns the parent as type
Container
because
only a subclass of
Container
can hold other components. If there is no parent, as is the case with a
JFrame
component, this method returns
null
.
Component Attributes
The
Component
class defines attributes, which record the following information about an object:
• The
position
is stored as (
x
,
y
) coordinates. This fixes where the object is in relation to its contain-
er in the coordinate system of the container object.
• The
name
of the component is stored as a
String
object.
• The
size
is recorded as values for the width and the height of the object.
• The
foreground color
and
background color
that apply to the object are used when the object is
displayed.