title: Using button in a panel class to call ActionListener in the frame class
body: <p>I started codig Java last weekend and I've read most of the basic stuff. I'm trying to separate my frame from main method, and panels from the frame so they are all in separate class files. I have trouble calling ActionLister in "Frame1" class with a button (buttonBack) in the "TheGame" class. The button should trigger the Listener which in turn should remove theGame panel and add mainMenu panel to frame1. I know that CardLayout is better suited for swapping panels but i want to learn the limits and workarounds before i go do it the "easy" way, i feel that you learn much more that way. </p>
  
  <p>Here is some of my code:</p>
  
  <p>Main:</p>
  
  <pre><code>public class Main {
  
  
  public static void main(String[] args){
  
      Frame1 frame1 = new Frame1();
      frame1.frame1();
  
      }
  }
  </code></pre>
  
  <p>Frame1:</p>
  
  <pre><code>import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JDialog;
  import javax.swing.JFrame;
  import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JOptionPane;
  
  
  class Frame1 {
  
  private JFrame frame1 = new JFrame("Name");
  
  public void frame1() {
  
      TheGame theGame = new TheGame();
      MainMenu mainMenu = new MainMenu();
  
  // Frame options
      frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame1.setLocationRelativeTo(null);
  
  
  // Creating a top menu
      JMenuBar menubar = new JMenuBar();
      frame1.setJMenuBar(menubar);
  
      JMenu file = new JMenu("File");
      menubar.add(file);
  
      JMenu help = new JMenu("Help");
      menubar.add(help);
  
      JMenuItem exit = new JMenuItem("Exit");
      file.add(exit);
  
      JMenuItem about = new JMenuItem("About");
      help.add(about);
  
  
  // Creating action for the menuitem "exit".
  class exitaction implements ActionListener{
      public void actionPerformed (ActionEvent e){
          System.exit(0);
      }
  }
  exit.addActionListener(new exitaction());
  
  
  // Creating listener for the menuitem "about".
      class aboutaction implements ActionListener{
          public void actionPerformed (ActionEvent e){
              JDialog dialogabout = new JDialog();
              JOptionPane.showMessageDialog(dialogabout, "Made by: ");
          }
      }
      about.addActionListener(new aboutaction());
  
  
  // Add the panels, pack and setVisible
      theGame.theGame();
      mainMenu.mainMenu();
      frame1.add(theGame.getGUI());
  
  
  // This is the ActionListener i have trouble connecting with the buttonBack in the "theGame" class
      class Action implements ActionListener{
          public void actionPerformed (ActionEvent e){
              frame1.remove(theGame.getGUI());
              frame1.add(MainMenu.getGUI());
          }
      }
  
      frame1.pack();
      frame1.setVisible(true);
  
  
  
      }
  
      public JFrame getGUI() {
          return frame1;
      }
  
  
  
  }
  </code></pre>
  
  <p>MainMenu:</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  
  import javax.swing.JButton;
  import javax.swing.JComponent;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  class MainMenu {
  
  private JPanel mainMenu = new JPanel (new GridBagLayout());
  
  public void mainMenu() {
  
  // Using the GridBagLayout therefore creating the constraints "grid"
      GridBagConstraints grid = new GridBagConstraints();
  
  // Adjusting grid insets
      grid.insets = new Insets(10, 10, 10, 10);
  
  
  // Creating Label
      JLabel introduction = new JLabel("Name");
      grid.gridx = 1;
      grid.gridy = 3;
      mainMenu.add(introduction, grid);
  
  
  // Creating buttons Start Game, Highscore and Exit Game
      JButton buttonNewGame = new JButton("New Game");
      buttonNewGame.setPreferredSize(new Dimension(200, 50));
      grid.gridx = 1;
      grid.gridy = 5;
      mainMenu.add(buttonNewGame, grid);
  
      JButton buttonHighscore = new JButton("Highscore");
      buttonHighscore.setPreferredSize(new Dimension(200, 50));
      grid.gridx = 1;
      grid.gridy = 6;
      mainMenu.add(buttonHighscore, grid);
  
      JButton buttonExit = new JButton("Exit Game");
      buttonExit.setPreferredSize(new Dimension(200, 50));
      grid.gridx = 1;
      grid.gridy = 7;
      mainMenu.add(buttonExit, grid);
  
  
      }
  
  
      public JComponent getGUI() {
          return mainMenu;
      }
  }
  </code></pre>
  
  <p>TheGame:</p>
  
  <pre><code>import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  
  import javax.swing.JButton;
  import javax.swing.JComponent;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  
  class TheGame {
  
  private JPanel theGame = new JPanel (new GridBagLayout());
  
  public void  theGame() {
  
  // Using the GridBagLayout therefore creating the constraints "grid"
      GridBagConstraints grid = new GridBagConstraints();
  
  // Adjusting grid insets
      grid.insets = new Insets(10, 10, 10, 10);
  
  // Creating a label
      JLabel label1 = new JLabel("Press the BACK button to go back to Main Menu");
      label1.setVisible(true);
      grid.gridx = 1;
      grid.gridy = 0;
      theGame.add(label1,grid);
  
  // Creating BACK button
      JButton buttonBack = new JButton("BACK");
      buttonBack.setVisible(true);
      grid.gridx = 1;
      grid.gridy = 1;
      buttonBack.addActionListener(new --); // This is the button i want to connect with the ActionListener on Frame1 class
      theGame.add(buttonBack, grid);
  
      }
  
  
      public JComponent getGUI() {
          return theGame;
      }
  
  
  }
  </code></pre>
  
  <p>I've tried moving the ActionListener outside of methods, inside the Main, declaring it static, but haven't been able to call it anyways. I've also looked at other posts like this: <a href="https://stackoverflow.com/questions/19104014/add-an-actionlistener-to-a-jbutton-from-another-class">Add an actionListener to a JButton from another class</a> but have not been able to implement it in to my code.</p>
  
  <p>Any help is appreciated.</p>