java - Need Help Getting A Simple Stop-Watch Working? -
so experimenting little stop-watch object , can't figure out why isn't working. have gui following; timeinfield jtextfield user enters start time of clock. timerfield jtextfield clock displayed timing. startbutton jbutton user clicks start clock. stopbutton jbutton user clicks stop clock.
right now, timerfield displays running: 0 seconds. nothing else happens, maybe doing wrong first time trying this. start learning how use threads thought might me started.
private void startbuttonactionperformed(java.awt.event.actionevent evt) { string starttim = timeinfield.gettext(); long starttime = long.valueof(starttim); boolean running = false; timer timer = null; long time = (system.currenttimemillis() - starttime)/1000; timerfield.settext("running:" + time + "seconds"); if (running==false) { running = true; timerfield.settext("running: 0 seconds"); } if (stopbutton.isselected()) { timer.stop(); running = false; long endtime = evt.getwhen(); double seconds = (endtime - starttime)/1000.0; timerfield.settext("time: " + seconds + "sec."); } }
it displays "running: 0 seconds" because after timerfield.settext("running: 0 seconds");
never update gui.
also, attempt perform timer.stop();
while timer null, cause nullpointerexception.
and 1 more thing: running false every time click button, since local variable , initialized false every time. if want permanent, move out of method, , initialize outside.
Comments
Post a Comment