Java Reference
In-Depth Information
cardlayout
The CardLayout layout manager is useful when you have two or more components (usually JPanel
objects) that should share the same display space and when the user can choose which component
should be shown.
Constructing it is very easy:
public CardLayout();
public CardLayout(int hgap, int vgap);
// hgap, vgap: horizontal/vertical gap between the components
The following Try It Out shows how it works.
playing Your Cards right with the CardLayout
try it out
In this Try It Out, you will get to see the CardLayout layout manager in use.
1.
As always, feel free to create a new project in Eclipse when you want to. Create a class called
CardLayoutFrame with the following content:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("CardLayout frame");
JPanel cardPanel = new JPanel();
cardPanel.setLayout(new CardLayout());
cardPanel.setPreferredSize(new Dimension(300, 400));
JPanel bluePanel = new JPanel();
JPanel redPanel = new JPanel();
bluePanel.setBackground(Color.blue);
redPanel.setBackground(Color.red);
cardPanel.add(bluePanel, "BLUE PANEL");
cardPanel.add(redPanel, "RED PANEL");
JPanel comboBoxPanel = new JPanel();
String comboBoxItems[] = { "BLUE PANEL", "RED PANEL" };
 
Search WWH ::




Custom Search