Studyon Minte9.com
Java SE




Card Layout




package testexample; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends JFrame implements ItemListener { // --- Look Here --- // private static JPanel cardsPanel = new JPanel(new CardLayout()); public static final String CARD1 = "Card 1"; public static final String CARD2 = "Card 2"; @Override public void itemStateChanged(ItemEvent e) { // --- Look Here --- // CardLayout c = (CardLayout) cardsPanel.getLayout(); c.show(cardsPanel, (String) e.getItem()); } public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 300, 200); frame.setLayout(new BorderLayout()); // default, can be ommited frame.setDefaultCloseOperation(EXIT_ON_CLOSE); // Combo Box JPanel topPanel = new JPanel(); String items[] = {CARD1, CARD2}; JComboBox comboBox = new JComboBox(items); topPanel.add(comboBox); comboBox.addItemListener(frame); // --- Look Here --- // // Cards (panels) JPanel panel1 = new JPanel(); panel1.setBackground(Color.red); panel1.setPreferredSize(new Dimension(100, 100)); JPanel panel2 = new JPanel(); panel2.setBackground(Color.blue); panel2.setPreferredSize(new Dimension(100, 100)); // Add panels to Card Layout // --- Look Here --- // // --- CARD1 & CARD2 are the index contrains // --- CARD1 & CARD2 must be the same as in ComboBox cardsPanel.add(panel1, CARD1); cardsPanel.add(panel2, CARD2); frame.getContentPane().add(topPanel, BorderLayout.PAGE_START); frame.getContentPane().add(cardsPanel, BorderLayout.CENTER); frame.setVisible(true); } }
 


http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html