|
|
Laying Out Components Within a Container |
Here's an applet that shows aCardLayoutin action.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.As the preceding applet shows, the
CardLayoutclass helps you manage two or more components (usuallyJPanelinstances) that share the same display space. Another way to accomplish the same thing is to use aJTabbedPane.
Conceptually, each component a
CardLayoutmanages is like a playing card or trading card in a stack, where only the top card is visible at any time. You can choose the card that's showing in any of the following ways:
- By asking for either the first or last card, in the order it was added to the container.
- By flipping through the deck backwards or forwards.
- By specifying a card with a specific name. This is the scheme the example program uses. Specifically, the user can choose a card (component) by selecting it by name from a pop-up list of choices.
Below is the code that creates the
CardLayoutand the components it manages. (Here's the whole program. The program runs either within an applet, with the help ofAppletButton, or as an application.)When you add a component to a container that a//Where instance variables are declared: JPanel cards; final static String BUTTONPANEL = "JPanel with JButtons"; final static String TEXTPANEL = "JPanel with JTextField"; //Where the container is initialized: cards = new JPanel(); cards.setLayout(new CardLayout()); ...//Create a Panel named p1. Put buttons in it. ...//Create a Panel named p2. Put a text field in it. cards.add(BUTTONPANEL, p1); cards.add(TEXTPANEL, p2);CardLayoutmanages, you must use the two-argument form of theContaineraddmethod:add(String name, Component comp). The first argument can be any string that somehow identifies the component being added.To choose which component a
CardLayoutshows, you need some additional code. Here's how the example program does this:This example shows that you can use the//Where the container is initialized: . . . //Put the JComboBox in a JPanel to get a nicer look. String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; JPanel cp = new JPanel(); JComboBox c = new JComboBox(comboBoxItems); c.setEditable(false); c.addItemListener(this); cp.add(c); contentPane.add(cp, BorderLayout.NORTH); . . . public void itemStateChanged(ItemEvent evt) { CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, (String)evt.getItem()); }CardLayoutshowmethod to set the currently showing component. The first argument to theshowmethod is the container theCardLayoutcontrols, that is, the container of the components theCardLayoutmanages. The second argument is the string that identifies the component to show. This string is the same as was used when adding the component to the container.The following are all the
CardLayoutmethods that let you choose a component. For each method, the first argument is the container for which theCardLayoutis the layout manager (the container of the cards theCardLayoutcontrols).public void first(Container parent) public void next(Container parent) public void previous(Container parent) public void last(Container parent) public void show(Container parent, String name)Examples that Use CardLayout
Only one example in this trail usesCardLayout:CardWindow.java. Generally, our examples use tabbed panesinstead of
CardLayout, since tabbed panes conveniently provide a nice GUI for the same functionality.
|
|
Laying Out Components Within a Container |