generally useful top-level container classes: JFrame
, JDialog
, and JApplet
.
l Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.
l You can optionally add a menu bar to a top-level container
The frame contains a green menu bar (with no menus) and, in the frame's content pane, a large blank, yellow label
frame.setJMenuBar(greenMenuBar);
Each top-level container relies on a reclusive intermediate container called the root pane. The root pane manages the content pane and the menu bar, along with a couple of other containers.
Here's a list of the components that a root pane provides to a frame (and to every other top-level container):
As the preceding figure shows, a root pane has four parts:
1. The glass pane
Hidden, by default. If you make the glass pane visible, then it's like a sheet of glass over all the other parts of the root pane. It's completely transparent unless you implement the glass pane's paintComponent method so that it does something, and it can intercept input events for the root pane. In the next section, you'll see an example of using a glass pane.
2. The layered pane
Serves to position its contents, which consist of the content pane and the optional menu bar. Can also hold other components in a specified Z order. For information, see The Layered Pane.
3. The content pane
The container of the root pane's visible components, excluding the menu bar. For information on using the content pane, see Using Top-Level Containers.
4. The optional menu bar
The home for the root pane's container's menus. If the container has a menu bar, you generally use the container's setJMenuBar method to put the menu bar in the appropriate place. For more information on using menus and menu bars, see How to Use Menus.