title: How to Switch between Panels in CardLayout from ActionListeners in external classes
body: <p>I have a cardLayout in the main class that the Gui classes are added to layout via panels and when the Room1Button is pressed how would it switch the card in the main method to the Gui2 card</p>
  
  <p>is this the best way to go about this any help would be apreatiated</p>
  
  <p>Main Method</p>
  
  <pre><code>  import javax.swing.*;
    import java.awt.*;
    class Main
    {
  
  CardLayout cl=new CardLayout();
  GridBagConstraints gb=new GridBagConstraints();
  JFrame frame=new JFrame("Frame");
  JPanel panel =new JPanel();
  
  
  Gui1 g1= Gui1();
  Gui2 g2= Gui2();
  
  
  public Main()
  {
     panel.setLayout(cl);
     panel.add(g1, "1");
     panel.add(g2, "2");
  
      frame.add(panel);
      frame.pack();
      frame.setVisible(true);
  
      cl.show(panel,"1");
  
      //how would the actionlistner in the Gui1 class switch the layout to "2"
  
      cl.show(panel, "2");
  
  
  }
  
  
  public static void main(String[]param)
  {
      new Main();
  
  
  }
  
  
  }
  </code></pre>
  
  <p>The gui1 class</p>
  
  <pre><code> import javax.swing.*;
   import java.awt.*;
   import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
   import java.util.*;
  
  
   public class Gui1 extends JPanel implements ActionListener{
  
  
  private JButton room1Button;
  
  JPanel panel=new JPanel();
  
  {
  
  
  
      setSize(1000,1000);
  
      panel.setVisible(true);
  
      room1Button=new JButton("Go the next Panel");
  
      this.setVisible(true);
      room1Button.addActionListener(this);
      add(room1Button);
  
  
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
  
      if(e.getSource()==room1Button){
         Window w = SwingUtilities.getWindowAncestor(R0.this);
         w.setVisible(false);
  
      }
  
  }
  }
  </code></pre>
  
  <p>The Gui2 class</p>
  
  <pre><code> public class Gui2 extends JPanel implements Actionlistener
   {
          // some code
  
   }
  </code></pre>