Java Swing Timer Not Starting? -


i wrote program test out swing's timer. compiles fine , has no run-time errors, stops starts , nothing pops on screen. it's modified version of example code found online, should work fine. have no idea what's wrong code:

import java.awt.event.*; import javax.swing.*;  public class time  { int sec=0; int min=0;  public time() {     actionlistener taskperformer = new actionlistener()     {         int sec=0;         int min=0;         public void actionperformed(actionevent evt)          {             if(sec==60)             {                 sec = 0;                 min++;             }             system.out.println("time passed: " + min + ":" + sec);             if(min==2)                 system.exit(0);         }     };     timer timer = new timer( 1000 , taskperformer);     timer.start(); }  public static void main(string[] args) {     time timer = new time(); } } 

javax.swing.timer uses daemon thread @ it's core. means once main method exists, jvm terminated.

you try using java.util.timer or creating kind of frame edt started

side note:

you're not incrementing sec variable

for example...

import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class time {      int sec = 0;     int min = 0;      public time() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                     ex.printstacktrace();                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.pack();                 frame.setlocationrelativeto(null);                  actionlistener taskperformer = new actionlistener() {                     int sec = 0;                     int min = 0;                      public void actionperformed(actionevent evt) {                         if (sec >= 60) {                             sec = 0;                             min++;                         }                         system.out.println("time passed: " + min + ":" + sec);                         if (min == 2) {                             system.out.println("terminate");                             system.exit(0);                         }                         sec++;                     }                 };                 timer timer = new timer(1000, taskperformer);                 timer.start();             }         });     }      public static void main(string[] args) {         time timer = new time();     } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -