import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LayoutTest { public static void main(String[] args) { LayoutTestFrame test = new LayoutTestFrame(); JPanel cards = new JPanel(); CardLayout cl = new CardLayout(5,5); cards.setLayout(cl); class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent evt) { CardLayout layout = (CardLayout) cards.getLayout(); layout.next(cards); } } // the listener ButtonHandler bh = new ButtonHandler(); // add components JButton ok = new JButton("ok"); ok.setBackground(Color.green); ok.setOpaque(true); ok.addActionListener(bh); JButton cancel = new JButton("cancel"); cancel.setBackground(Color.red); cancel.setOpaque(true); cancel.addActionListener(bh); JPanel questionPanel = new JPanel(); JLabel question = new JLabel("Save file?"); question.setBackground(Color.yellow); question.setOpaque(true); JButton next = new JButton("next"); next.addActionListener(bh); questionPanel.add(question); questionPanel.add(next); cards.add(questionPanel); cards.add(ok); cards.add(cancel); test.getContentPane().add(cards); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setVisible(true); } } class LayoutTestFrame extends JFrame { public LayoutTestFrame() { setBackground(Color.lightGray); setSize(400,300); setLocation(100,100); } }