I'm having a problem in my program. I want to get a real time millisecond that equal to 1000 in 1 seconds. Here's my code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
class lsen implements ActionListener{
int ms = 0;
int s = 0;
int m = 0;
public void actionPerformed(ActionEvent e){
this.ms++;
if(this.ms == 500){
this.s++;
this.ms = 0;
}
if(this.s == 60){
this.m++;
this.s = 0;
}
}
public int getMS(){
return this.ms;
}
public int getSS(){
return this.s;
}
public int getMM(){
return this.m;
}
}
public class stopwatch_main{
public static void main(String[] args) {
lsen l = new lsen();
Timer t = new Timer(0,l);
t. start();
while(true){
System.out.println(l.getMM()+":"+l.getSS()+":"+l.getMS());
}
}
}
Is there any other way in getting the millisecond rather than declaring a integer and incrementing it?
while (true)
and instead use the Swing Timer in its place since that is what the Timer is for -- to repeatedly make calls in a Swing GUI without having to resort to a thread-breakingwhile (true)
construct.For example:
Edit
You ask about the meaning of the long data type. Please have a look here: Primitive Data Types. You'll see that long means long integer, and so you can think of it as being similar to int but able to tolerate much larger positive and negative values without overflowing.