1

Below is the code I've got. I'm trying to create an array of 6 random numbers and 30 check boxes. In the action listener, I want to validate that the six boxes the user clicked happen to be my six random numbers. However, I can't pull down my random number array to the actionlistener. Can someone give me some guidance please? I'm a total newb at this. The int[] rand array is what is giving me grief.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class JLottery2 extends JFrame implements ActionListener
{
   int actionCounter = 0;
   int matchTally = 0;

   final int MAXBOXES = 6; 
   final int WIDTH = 500; 
   final int HEIGHT = 200;
   JCheckBox[] boxes = new JCheckBox [30];

   public JLottery2()
   {
      super ("~*~*~ JLOTTERY 2 ~*~*~");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout (new FlowLayout());
      setSize(WIDTH, HEIGHT);

      System.out.println("Constructor");

      //great the check boxes and assign them a value      
      for (int count=0 ; count < 30; count++)
      {
         boxes[count] = new JCheckBox (Integer.toString(count));
         add(boxes[count]);
         boxes[count].addActionListener(this);
      }

      int[] rand = new int[MAXBOXES]; 
      for (int i = 0; i < MAXBOXES; i++)
      {
         rand[i] = (int)(Math.random() * 30);
         //incase it tries to generate the same random number 
         for (int j = 0; j < i; j++)
         {
            if(rand[i] == rand[j])
            {
              i--; 
            }
         }
      }


      setVisible(true);
   }

   public void actionPerformed(ActionEvent e, )
   {      
      System.out.println(rand[5]);

      if(actionCounter < MAXBOXES)
      {
         System.out.println("Action Triggered");
         Object source = e.getActionCommand();
         System.out.println(source);         
         actionCounter++;
         System.out.println(actionCounter);   
      }
      else 
      {
         System.out.println("You reached the max at " + actionCounter);
      }

   } 

   public static void main(String[] args)
   {
      JLottery2 prog = new JLottery2();
   }
}
4
  • Make int[] rand a class instance field like JCheckBox[] boxes Commented Jul 10, 2015 at 4:13
  • Consider using pack over setSize(WIDTH, HEIGHT); (but only after you've actually finished creating the UI) Commented Jul 10, 2015 at 4:14
  • @MadProgrammer I can't put the for loop up there though? Commented Jul 10, 2015 at 4:22
  • I didn't say the loop, I said the declaration of the field. Commented Jul 10, 2015 at 4:25

2 Answers 2

1

Make rand an class instance field like boxes

public class JLottery2 extends JFrame implements ActionListener
{
     //...
     JCheckBox[] boxes = new JCheckBox [30];
     int[] rand;

     public JLottery2()
     {
         //...
         rand = new int[MAXBOXES]; 

This now gives you a class level context to the field, which allows you to access from anywhere within the class

Sign up to request clarification or add additional context in comments.

Comments

1

The actionPerformed method cannot access rand as rand is declared in the local scope of the JLottery2 constructor. If rand is declared in a global scope, as in, outside of all methods (just as MAXBOXES, WIDTH, HEIGHT, etc. are), then it would be accessible. Note that you can still initialize it within the constructor:

public class JLottery2 extends JFrame implements ActionListener
{
    int actionCounter = 0;
    int matchTally = 0;
    int[] rand;
    ......
    public JLottery2()
    {
        ....
        rand = new int[MADBOXES];

EDIT: Here's a good link for you to check out. It explains scope with basic Java code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.