Java GUI/Swing, stopping the thread, passing integers between classes -
Java GUI/Swing, stopping the thread, passing integers between classes -
i'm trying create simulation of roulette game using gui/swing upcoming exam. have 2 classes, 1 called gui , code used components, such jframe, joptionpane, jbuttons etc. other 1 extends thread , supposed show random numbers on little jlabel, , run method goes this:
public void run() { int k = 0; (int = 0; < 50; i++) { k = (new random().nextint(37)); label.settext(k + " "); label.setfont(new font("tahoma", font.bold, 56)); label.setforeground(color.yellow); seek { sleep(50); } grab (interruptedexception e) { e.printstacktrace(); } } }
and in gui class want take number lastly iteration of above loop, , pass new int, i'm going utilize later in gui class. ideas?
use swing timer instead of thread.sleep
sometime hangs whole swing application.
please have @ how utilize swing timers
timer timer = new timer(50, new actionlistener() { @override public void actionperformed(actionevent arg0) { //next phone call here } }); timer.setrepeats(false); timer.start();
i want take number lastly iteration of above loop, , pass new int, i'm going utilize later in gui class.
just create method (setter) in class accepts int , phone call class lastly call.
sample code:
private int counter = 0; private timer timer; ... final jlabel label = new jlabel(); label.setfont(new font("tahoma", font.bold, 56)); label.setforeground(color.yellow); thread thread = new thread(new runnable() { public void run() { timer = new timer(50, new actionlistener() { @override public void actionperformed(actionevent e) { if (counter++ < 50) { int k = (new random().nextint(37)); label.settext(k + " "); } else { timer.stop(); label.settext("next call"); } } }); timer.setrepeats(true); timer.start(); } }); thread.start();
snapshot:
java swing user-interface
Comments
Post a Comment