java - thread doesn't execute -
i'm new in java, guess it's simple problem. in event listener have method declares variables , i'm trying use variables in thread use in same event. although not work, here code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class gui extends frame implements windowlistener,actionlistener { cpwindow window1 = new cpwindow(); cmwindow window2 = new cmwindow(); solarsystem system = new solarsystem(300,300); planet planetarium; /** * frame general window. */ public void createframe0(){ jframe f0 = new jframe("choose want do"); f0.setdefaultcloseoperation(jframe.exit_on_close); f0.addwindowlistener(this); f0.setlayout(new gridlayout(3,1)); jbutton cp = new jbutton("create planet"); jbutton cm = new jbutton("create moon"); jbutton delete = new jbutton("delete planet or moon"); f0.add(cp); f0.add(cm); f0.add(delete); cp.addactionlistener(this); cm.addactionlistener(this); delete.addactionlistener(this); cp.setactioncommand("1"); cm.setactioncommand("2"); delete.setactioncommand("3"); f0.pack(); f0.setvisible(true); } /** * frame planet adding window. */ class cpwindow implements actionlistener,windowlistener{ jlabel name1 = new jlabel("name"); jlabel color1 = new jlabel("color"); jlabel diam1 = new jlabel("diameter"); jlabel dist1 = new jlabel("distance"); jlabel speed1 = new jlabel("speed"); jtextfield name2 = new jtextfield(); jtextfield color2 = new jtextfield(); jtextfield diam2 = new jtextfield(); jtextfield dist2 = new jtextfield(); jtextfield speed2 = new jtextfield(); double distance; int speed; double diameter; public void createframe1() { jframe f1 = new jframe("add planet"); f1.addwindowlistener(this); f1.setlayout(new gridlayout(6,2,5,5)); jbutton mygt = new jbutton("create planet"); mygt.addactionlistener(this); name2.settext("belekoks");color2.settext("red");diam2.settext("30");dist2.settext("60");spe ed2.settext("2"); f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);f1.add(diam1); f1.add(diam2);f1.add(dist1);f1.add(dist2);f1.add(speed1);f1.add(speed2); f1.add(mygt); f1.pack(); f1.setvisible(true); } public void createvariables(){ try { distance = double.parsedouble(dist2.gettext()); speed = integer.parseint(speed2.gettext()); diameter = double.parsedouble(diam2.gettext()); } catch(numberformatexception i) { } main.diametras = diameter; main.distancija = distance; main.greitis = speed; main.vardas = name2.gettext(); main.spalva = color2.gettext(); planetarium = new planet(main.vardas,main.distancija,main.diametras,main.spalva,main.greitis); } public void actionperformed(actionevent e) { createvariables(); new newthread().run(); } public void windowclosing(windowevent e) { dispose(); system.exit(0); } public void windowclosed(windowevent e) {} public void windowactivated(windowevent arg0) {} public void windowdeactivated(windowevent arg0) {} public void windowdeiconified(windowevent arg0) {} public void windowiconified(windowevent arg0) {} public void windowopened(windowevent arg0) {} } /** * frame moon adding window */ class cmwindow implements actionlistener,windowlistener{ jlabel name1 = new jlabel("name"); jlabel color1 = new jlabel("color"); jlabel diam1 = new jlabel("diameter"); jlabel dist1 = new jlabel("distance"); jlabel speed1 = new jlabel("speed"); jlabel cordist1 = new jlabel("distance centre of rotation"); jlabel corspeed1 = new jlabel("speed moon centres"); jtextfield name2 = new jtextfield(); jtextfield color2 = new jtextfield(); jtextfield diam2 = new jtextfield(); jtextfield dist2 = new jtextfield(); jtextfield speed2 = new jtextfield(); jtextfield cordist2 = new jtextfield(); jtextfield corspeed2 = new jtextfield(); double distance; int speed; double diameter; int cordistance; int corspeed; public void createframe1() { jframe f1 = new jframe("add moon"); f1.addwindowlistener(this); f1.setlayout(new gridlayout(8,2,5,5)); jbutton mygt = new jbutton("create moon"); mygt.addactionlistener(this); name2.settext("belekoks");color2.settext("blue");diam2.settext("15");dist2.settext("60");sp eed2.settext("2");cordist2.settext("15");corspeed2.settext("3"); f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);f1.add(diam1); f1.add(diam2);f1.add(dist1);f1.add(dist2);f1.add(speed1);f1.add(speed2); f1.add(cordist1);f1.add(cordist2);f1.add(corspeed1);f1.add(corspeed2); f1.add(mygt); f1.pack(); f1.setvisible(true); } public void createvariables(){ try { distance = double.parsedouble(dist2.gettext()); speed = integer.parseint(speed2.gettext()); diameter = double.parsedouble(diam2.gettext()); cordistance = integer.parseint(cordist2.gettext()); corspeed = integer.parseint(corspeed2.gettext()); } catch(numberformatexception i) {} /* main.diametras = diameter; main.distancija = distance; main.greitis = speed; main.vardas = name2.gettext(); main.spalva = color2.gettext(); */ main.centgrt = corspeed; main.centats = cordistance; } public void actionperformed(actionevent e) { createvariables(); } public void windowclosing(windowevent e) { dispose(); system.exit(0); } public void windowclosed(windowevent e) {} public void windowactivated(windowevent arg0) {} public void windowdeactivated(windowevent arg0) {} public void windowdeiconified(windowevent arg0) {} public void windowiconified(windowevent arg0) {} public void windowopened(windowevent arg0) {} } /** * deleting window */ class delwindow implements actionlistener,windowlistener{ public void actionperformed(actionevent e) { } public void windowclosing(windowevent e) { dispose(); system.exit(0); } public void windowclosed(windowevent e) {} public void windowactivated(windowevent arg0) {} public void windowdeactivated(windowevent arg0) {} public void windowdeiconified(windowevent arg0) {} public void windowiconified(windowevent arg0) {} public void windowopened(windowevent arg0) {} } //////////////////////////////////////////////////////// public gui() { createframe0(); } public void actionperformed(actionevent e) { if ("1".equals(e.getactioncommand())) {this.window1.createframe1();} else if ("2".equals(e.getactioncommand())) {this.window2.createframe1();} // else if ("3".equals(e.getactioncommand())) {this.window2.createframe1();} } public void windowclosing(windowevent e) { dispose(); system.exit(0); } public void windowclosed(windowevent e) {} public void windowactivated(windowevent arg0) {} public void windowdeactivated(windowevent arg0) {} public void windowdeiconified(windowevent arg0) {} public void windowiconified(windowevent arg0) {} public void windowopened(windowevent arg0) {} //create new thread. class newthread implements runnable { thread t; newthread() { // create new, second thread t = new thread(this); t.start(); // start thread } // entry point second thread. public void run() { planet planet = new planet(main.vardas,main.distancija,main.diametras,main.spalva,main.greitis); try { for(int = 0; >= 0; i++) { planet.move(); planet.drawon(system); system.finisheddrawing(); } } catch (exception e) {} } } }
any ideas?
one needs start thread, instantiating thread object not enough.
public void actionperformed(actionevent e) { createvariables(); new newthread().start(); }
Comments
Post a Comment