I'm pretty new to java, and I just started writing this dumb little program as a GUI test. What it's supposed to do is have 12 buttons, set them all to white, continuously make 3 random buttons black, set all buttons back to white again, wait one second and then repeat. The problem is, I can't seem to make it repeat. Every time I try to put a while or for loop around the section of code that makes the random buttons black, it simply won't run. It doesn't give any errors, and the process itself does run, but no window appears. This is the code for the class (minus the import statements):
public class testingness extends JFrame {
JButton one, two, three, four, five, six, seven, eight, nine, ten, eleven,
twelve;
JPanel panel;
testingness(String title) {
super(title);
this.init();
this.setSize(800, 800);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
void init() {
panel = new JPanel();
panel.setLayout(new GridLayout(3, 4));
one = new JButton();
one.setBackground(Color.white);
two = new JButton();
two.setBackground(Color.white);
three = new JButton();
three.setBackground(Color.white);
four = new JButton();
four.setBackground(Color.white);
five = new JButton();
five.setBackground(Color.white);
six = new JButton();
six.setBackground(Color.white);
seven = new JButton();
seven.setBackground(Color.white);
eight = new JButton();
eight.setBackground(Color.white);
nine = new JButton();
nine.setBackground(Color.white);
ten = new JButton();
ten.setBackground(Color.white);
eleven = new JButton();
eleven.setBackground(Color.white);
twelve = new JButton();
twelve.setBackground(Color.white);
panel.add(one);
panel.add(two);
panel.add(three);
panel.add(four);
panel.add(five);
panel.add(six);
panel.add(seven);
panel.add(eight);
panel.add(nine);
panel.add(ten);
panel.add(eleven);
panel.add(twelve);
this.add(panel);
while (true) {
randomness();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void randomness() {
for (int timesdone = 0; timesdone < 4; timesdone++) {
panel.update(panel.getGraphics());
Random r = new Random();
int rand = r.nextInt(12);
if (rand == 0) {
one.setBackground(Color.black);
} else if (rand == 1) {
two.setBackground(Color.black);
} else if (rand == 2) {
three.setBackground(Color.black);
} else if (rand == 3) {
four.setBackground(Color.black);
} else if (rand == 4) {
five.setBackground(Color.black);
} else if (rand == 5) {
six.setBackground(Color.black);
} else if (rand == 6) {
seven.setBackground(Color.black);
} else if (rand == 7) {
eight.setBackground(Color.black);
} else if (rand == 8) {
nine.setBackground(Color.black);
} else if (rand == 9) {
ten.setBackground(Color.black);
} else if (rand == 10) {
eleven.setBackground(Color.black);
} else if (rand == 11) {
twelve.setBackground(Color.black);
}
one.setBackground(Color.white);
two.setBackground(Color.white);
three.setBackground(Color.white);
four.setBackground(Color.white);
five.setBackground(Color.white);
six.setBackground(Color.white);
seven.setBackground(Color.white);
eight.setBackground(Color.white);
nine.setBackground(Color.white);
ten.setBackground(Color.white);
eleven.setBackground(Color.white);
twelve.setBackground(Color.white);
}
}
}
What am I doing wrong here?