swing - Problems with loops in GUI (java) -
i'm pretty new java, , started writing dumb little program gui test. it's supposed have 12 buttons, set them white, continuously make 3 random buttons black, set buttons white again, wait 1 second , repeat. problem is, can't seem make repeat. every time try put while or loop around section of code makes random buttons black, won't run. doesn't give errors, , process run, no window appears. code class (minus 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)); 1 = new jbutton(); one.setbackground(color.white); 2 = new jbutton(); two.setbackground(color.white); 3 = new jbutton(); three.setbackground(color.white); 4 = new jbutton(); four.setbackground(color.white); 5 = new jbutton(); five.setbackground(color.white); 6 = new jbutton(); six.setbackground(color.white); 7 = new jbutton(); seven.setbackground(color.white); 8 = new jbutton(); eight.setbackground(color.white); 9 = 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() { (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 doing wrong here?
as germann arlington pointed out, gui thread never being allowed run because constructor never returns. however, better means of getting performed on regular basis on gui thread (which need change attributes of gui components) use timer (http://docs.oracle.com/javase/7/docs/api/javax/swing/timer.html). e.g.:
int delay = 1000; //milliseconds actionlistener taskperformer = new actionlistener() { public void actionperformed(actionevent evt) { randomness(); } }; new timer(delay, taskperformer).start();