title: Can't get CardLayout/button ActionListeners to Work
body: <p>I know it's something to do with how I've set it up and the actionlistener not being correctly set to the frame or something but I just can't get my hear around it. If someone could point me in the right direction I'd be much obliged. Sorry for noob question.</p>
  
  <p>Here's what I have:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class Main implements ActionListener {
  
      JPanel cardHolder;
      public static final String HOME_CARD = "Home";
      public static final String BLUE_PANEL = "Blue Panel";
      public static final String RED_PANEL = "Red Panel";
      public static final String ORANGE_PANEL = "Orange Panel";
  
      public static JButton home = new JButton("Home");
      public static JButton bluePanel = new JButton("Blue Card");
      public static JButton redPanel = new JButton("Red Panel");
      public static JButton orangePanel = new JButton("Orange Panel");
  
      public static JPanel createCardHolderPanel() {
          JPanel cardHolder = new JPanel(new CardLayout());
          cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
  
          cardHolder.add(createHomeCard(), HOME_CARD);
          cardHolder.add(createBluePanel(), BLUE_PANEL);
          cardHolder.add(createRedPanel(), RED_PANEL);
          cardHolder.add(createOrangePanel(), ORANGE_PANEL);
  
          return cardHolder;
      }
  
      private static JPanel createOrangePanel() {
          JPanel orangePanel = new JPanel();
  
          orangePanel.setBackground(Color.orange);
          return orangePanel;
      }
  
      private static Component createRedPanel() {
          JPanel redPanel = new JPanel();
  
          redPanel.setBackground(Color.red);
          return redPanel;
      }
  
      private static Component createBluePanel() {
          JPanel bluePanel = new JPanel();
  
          bluePanel.setBackground(Color.blue);
          return bluePanel;
      }
  
      private static Component createHomeCard() {
          JPanel homePanel = new JPanel();
  
          homePanel.setBackground(Color.GRAY);
          return homePanel;
      }
  
      public static JPanel createButtonPanel() {
          JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
          buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
  
          buttonPanel.add(home);
          buttonPanel.add(bluePanel);
          buttonPanel.add(redPanel);
          buttonPanel.add(orangePanel);
  
          return buttonPanel;
      }
  
      public static JPanel createContentPane() {
          JPanel contentPane = new JPanel(new BorderLayout());
  
          contentPane.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
          contentPane.setBackground(Color.WHITE);
          contentPane.setPreferredSize(new Dimension(499, 288));
  
          contentPane.add(createButtonPanel(), BorderLayout.WEST);
          contentPane.add(createCardHolderPanel(),BorderLayout.CENTER);
  
          return contentPane;
      }
  
      public static JMenuBar createMenuBar() {
          JMenuBar menuBar = new JMenuBar();
  
          JMenu file = new JMenu("File");
          JMenu users = new JMenu("Users");
          JMenu options = new JMenu("Options");
          JMenu help = new JMenu("Help");
  
          menuBar.add(file);
          menuBar.add(users);
          menuBar.add(options);
          menuBar.add(help);
  
          return menuBar;
      }
  
      public static void createAndShowGUI() {
          JFrame frame = new JFrame("Simple CardLayout Program");
          frame.setContentPane(createContentPane());
          frame.setJMenuBar(createMenuBar());
          frame.pack();
          frame.setVisible(true);
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI();
              }
          });
      }
  
      @Override
      public void actionPerformed(ActionEvent e) {
  
          if (e.getSource() == home) {
              CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
              cardLayout.show(cardHolder, HOME_CARD);
          }
  
          if (e.getSource() == bluePanel) {
              CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
              cardLayout.show(cardHolder, BLUE_PANEL);
          }
  
          if (e.getSource() == redPanel) {
              CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
              cardLayout.show(cardHolder, RED_PANEL);
          }
  
          if (e.getSource() == orangePanel) {
              CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
              cardLayout.show(cardHolder, ORANGE_PANEL);
          }
      }
  }
  </code></pre>