Java Reference
In-Depth Information
Container , so any descendent class of the class JFrame can serve as a container to hold
labels, buttons, panels, or other components.
Similarly, the class JPanel is a descendent of the class Container , and any object of
the class JPanel can serve as a container to hold labels, buttons, other panels, or other
components. Display 17.12 shows a portion of the hierarchy of Swing and AWT classes.
Note that the Container class is in the AWT library and not in the Swing library. This is
not a major issue, but it does mean that the import statement for the Container class is
import java.awt.Container;
container
class
component
A container class is any descendent class of the class Container . The class
JComponent serves a similar roll for components. Any descendent class of the class
JComponent is called a JComponent or sometimes simply a component . You can add
any JComponent object to any container class object.
The class JComponent is derived from the class Container , so you can add a
JComponent to another JComponent . Often, this will turn out to be a viable option;
occasionally it is something to avoid. 6
The classes Component , Frame , and Window shown in Display 17.12 are AWT classes
that some readers may have heard of. We include them for reference value, but we will
have no need for these classes. We will eventually discuss all the other classes shown in
Display 17.12.
When you are dealing with a Swing container class, you have three kinds of objects
to deal with:
1. The container itself, probably some sort of panel or window-like object
2. The components you add to the container, like labels, buttons, and panels
3. A layout manager, which positions the components inside the container
You have seen examples of these three kinds of objects in almost every JFrame class we
have defined. Almost every complete GUI you build, and many subparts of the GUIs
you build, will be made up of these three kinds of objects.
Self-Test Exercises
19. What standard Java package contains the layout manager classes discussed in this
chapter?
20. Is an object of the class JPanel a container class? Is it a component class?
21. With a GridLayout manager, you cannot leave any grid element empty, but you can
do something that will make a grid element look empty to the user. What can you do?
6 In particular, it is legitimate and sometimes useful to add JComponents to a JButton . We do not
have space in this topic to develop techniques for doing this effectively, but you may want to give it a
try. You have had enough material to do it.
Search WWH ::




Custom Search