title: Java: How do I pass variables from JButton ActionListener to main class?
body: <p>I am trying to make a program that generates a random number when a button is clicked and then the output is displayed on the screen. However, I can't pass the variable that holds the random number into the class with the JLabel so that it can be used in that class. I wrote a program that is as simple as I could make it to demonstrate:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Panel;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.Random;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  
  public class Main{
      public static void main(String[] args){
  
      JFrame mainFrame = new JFrame("Experiment");
      mainFrame.setSize(500,500);
      mainFrame.setVisible(true);
  
      Panel panel = new Panel();
      mainFrame.getContentPane().add(panel);
  
      JLabel output = new JLabel("This is where the result from the num variable in the numGenerator class would go"); 
      panel.add(output); 
  
      JButton numGenerator = new JButton("Generate Number");
      panel.add(numGenerator);
      numGenerator.addActionListener(new numGenerator());
  
      }
  
  static class numGenerator implements ActionListener{
      public void ActionPerfomed (ActionEvent e){
  
          int num; //This is the variable I want to be passed to the
                   //Main class so it can be displayed in the "output" Jlabel.
  
          Random dice = new Random();
          num = dice.nextInt(3);
  
      }
  
      @Override
      public void actionPerformed(ActionEvent arg0) {
          // TODO Auto-generated method stub
  
      }
  }
  }
  </code></pre>
  
  <p>I saw other help online to create an object of the class and then use that in the class you want the variable to be in, but I couldn't get that to work in this situation.</p>