Java Reference
In-Depth Information
(a)
(b)
Figure 7.11
(a) A
CardLayout
only shows one component at a time. (b) A
JTabbedPane
component, like this one with three tabbed pages, makes clear how
many panes are overlapping.
The components go into the five locations labeled according to the four com-
pass directions plus the center. The components that go into the
“
NORTH
„
and
“
SOUTH
„
locations will fill the horizontal space while maintaining their pre-
ferred vertical dimensions. The components that go into the
“
WEST
„
and
“
EAST
„
locations will fill the available vertical space while maintaining their preferred
horizontal dimensions. The component that goes into the center will fill up the
rest of the space in both horizontal and vertical dimensions.
If you want to maintain both the preferred horizontal and preferred vertical
dimensions of the components, you can put each component into its own
JPanel
and then in turn add these panels to a panel that uses the
BorderLayout
.
7.4.4
CardLayout
and
JTabbedPane
The
CardLayout
arranges components into a “vertical” stack where only the
top component is visible at a given time. Figure 7.11(a) shows the display for the
following code where we stack three buttons on top of each other:
public class
CardApplet
extends JApplet
{
. ..init()
builds the interface
...
}
// class CardApplet
/** Stack three buttons using CardLayout. **/
class
CardPanel
extends JPanel
implements ActionListener
{
CardLayout fCards;
/** Constructor adds three buttons to the panel
* and uses CardLayout.
**/
CardPanel () {
fCards = new CardLayout ();
setLayout (fCards);
Search WWH ::
Custom Search