Containers
A
container is a kind of component that holds and manages other
components. JComponent objects can be containers,
because the JComponent class descends from the
Container
class.
Three of the most useful container types are
JFrame
, JPanel, and
JApplet. A JFrame is a
top-level window on your display.
JFrame is derived from JWindow,
which is pretty much the same but lacks a border. A
JPanel is a generic container element used to
group components inside of JFrames and other
JPanels. The JApplet class is a
kind of container that provides the foundation for
applets that run inside web browsers.
Like every other JComponent, a
JApplet has the ability to contain other user
interface components. You can also use the
JComponent class directly, like a
JPanel, to hold components inside of another
container. With the exception of JFrame and
JWindow, all the components and containers in
Swing are lightweight.
A container maintains the list of
“child” components that it manages, and has
methods for dealing with those components. Note that this child
relationship refers to a visual hierarchy, not a subclass/superclass
hierarchy. By themselves, most components aren’t very useful
until they are added to a container and displayed. The add( ) method of the Container class adds a
component to the container. Thereafter, this component can be
displayed in the container’s display area and positioned by its
layout manager. You can remove a component from a container with the
remove( )
method. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access