Java Swing.Timer获得实时毫秒 [英] Java Swing.Timer get real time millisecond

查看:137
本文介绍了Java Swing.Timer获得实时毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序出了问题。我希望得到一个实时毫秒,在1秒内等于1000。这是我的代码:

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?

推荐答案


  1. 你会想要摆脱它那个而(true)而是在它的位置使用Swing Timer,因为这就是Timer的用途 - 在Swing GUI中重复进行调用 必须诉诸破解而(真实)构建。

  2. 你想要给你的计时器一个合理的延迟时间。 0?常识告诉你不要使用它。 12,15 - 更好。

  3. 要使Swing Timer工作,你需要有一个活动的Swing事件线程,这可以通过显示Swing GUI,任何GUI,如一个JOptionPane。


  1. You will want to get rid of that 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-breaking while (true) construct.
  2. You will want to give your Timer a reasonable delay time. 0? Common sense tells you not to use this. 12, 15 -- better.
  3. For a Swing Timer to work, you need to have an active Swing event thread, and this can be obtained by showing a Swing GUI, any GUI such as a JOptionPane.

例如:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

class Lsen implements ActionListener {
   public static final int MSECS_PER_SEC = 1000;
   public static final int SECS_PER_MIN = 60;
   public static final int MIN_PER_HR = 60;
   private static final String TIME_FORMAT = "%02d:%02d:%02d:%03d";

   private long startTime;
   private JTextField timeField;

   public Lsen(JTextField timeField) {
      this.timeField = timeField;
   }

   public void actionPerformed(ActionEvent e) {
      if (startTime == 0L) {
         startTime = System.currentTimeMillis();
      } else {
         long currentTime = System.currentTimeMillis();
         int diffTime = (int) (currentTime - startTime);

         int mSecs = diffTime % MSECS_PER_SEC;
         diffTime /= MSECS_PER_SEC;

         int sec = diffTime % SECS_PER_MIN;
         diffTime /= SECS_PER_MIN;

         int min = diffTime % MIN_PER_HR;
         diffTime /= MIN_PER_HR;

         int hours = diffTime;

         String time = String.format(TIME_FORMAT, hours, min, sec, mSecs);
         // System.out.println("Time: " + time);
         timeField.setText(time);
      }
   }
}

public class StopWatchMain {
   private static final int TIMER_DELAY = 15;

   public static void main(String[] args) {
      final JTextField timeField = new JTextField(10);
      timeField.setEditable(false);
      timeField.setFocusable(false);
      JPanel panel = new JPanel();
      panel.add(new JLabel("Elapsed Time:"));
      panel.add(timeField);

      Lsen l = new Lsen(timeField);
      Timer t = new Timer(TIMER_DELAY, l);
      t.start();
      JOptionPane.showMessageDialog(null, panel);
      t.stop();
   }
}






修改


您询问长数据类型的含义。请看这里:原始数据类型。你会看到long意味着长整数,所以你可以认为它类似于int但能够容忍更大的正负值而不会溢出。


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.

这篇关于Java Swing.Timer获得实时毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆