Java Reference
In-Depth Information
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 will use most frequently, since you normally add components
to the content pane.
getGlassPane()
Returns the glass pane as type
Component
.
All the classes discussed 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 we start delving into how to add a
menu bar or other components to a window, let's unearth a little more about the make-up of
components in general.
Basics of Components
There's quite a lot of basic stuff that is common to all components that we have to examine before we
can apply components properly. It also has applications in many different contexts. While this may
seem like something of a catalog of classes and methods, without much in the way of practical
application, please stay with it. We will be using most of these capabilities in a practical context later.
To understand the fundamental things we can do with Swing components, we will examine what
functionality they 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 find out what the parent of any given component is by calling its
getParent()
method. This
method is inherited from the
Component
class and it returns the parent as type
Container
, since only
a subclass of
Container
can hold other components. If there is no parent, as is the case with a
JFrame
component, this method will return
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
container 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. These color values are
used when the object is displayed.
❑
The
font
used by the object when text is displayed.
❑