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();
}
}
int[] randa class instance field likeJCheckBox[] boxespackoversetSize(WIDTH, HEIGHT);(but only after you've actually finished creating the UI)