简体   繁体   English

如何更新经过的时间

[英]How to update time elapsed

I'm developing a program which will count the time until the button is clicked I'm having trouble with using Timer. 我正在开发一个程序,该程序将计算单击按钮之前的时间,但我无法使用Timer。 I wanted to have something like 00:00:00 -> 00:00:01 -> 00:00:02 ... etc my problem is that 我想要像00:00:00-> 00:00:01-> 00:00:02 ...

it is stuck in 00:00:01 它卡在00:00:01

here's my code 这是我的代码

Timer timer=new Timer(1000,null);


JLabel time=new JLabel("00:00:00");
timer.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
         DecimalFormat df=new DecimalFormat("00");
         int h=0;
         int m=0;
         int s=0;
         s++;
         if(s==60)
         {
             m++;
             if(m==60)
             {
                  h++;
             }
         }
         time.setText(df.format(h)+":"+df.format(m)+":"+df.format(s));
         revalidate();
         repaint();
      }
   });
   timer.start();

You've declared your variables as local variables within the context of the ActionListener ... 您已经在ActionListener的上下文中将变量声明为局部变量。

@Override
public void actionPerformed(ActionEvent e)
{
     DecimalFormat df=new DecimalFormat("00");
     int h=0;
     int m=0;
     int s=0;

This means that each time actionPerformed the variables are reset to 0 ... 这意味着每次将actionPerformed变量重置为0 ...

Try making them instance variables instead... 尝试使它们成为实例变量...

You're also not resetting the variables when they pass their limits, for example... 例如,当变量超过限制时,您也不会重置它们。

s++;
if (s >= 60) {
    s = 0;
    m++;
    if (m >= 60) {
        h++;
        m = 0;
    }
}

As an alternative, you could maintain a single counter, which acts as the number of seconds that have passed and use some module mathematics to calculate the time parts 或者,您可以维护一个计数器,该计数器用作已过去的秒数,并使用一些模块数学来计算时间部分

private int count = 0;

//...

Timer timer = new Timer(1000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        count++;

        int hours = count / (60 * 60);
        float remainder = count % (60 * 60);
        float mins = remainder / (60);
        remainder = remainder % (60);
        float seconds = remainder;

        DecimalFormat df=new DecimalFormat("00");
        time.setText(df.format(hours) + ":" + df.format(mins) + ":" + df.format(seconds));

    }
});
timer.start();

This makes it a little simpler in the fact that you are managing a single value and then deciding how best to format it, rather then managing three states...IMHO 这使得您管理单个值,然后决定如何最好地设置其格式,而不是管理三个状态,这一点使事情变得更加简单。

If I understand you, you should first store the startTime - 如果我了解您,则应首先存储startTime-

final long startTime = System.currentTimeMillis();

Then use it to calculate your time fields in actionPerformed() , 然后使用它来计算actionPerformed()时间字段,

DecimalFormat df = new DecimalFormat("00");
long endTime = System.currentTimeMillis();
long diff = endTime - startTime;
int h = (int) (diff) / (60*60*1000);
diff -= h * (60*60*1000);
int m = (int) (endTime-startTime) / (60*1000);
diff -= m * (60 * 1000);
int s = (int) (diff / 1000);

time.setText(df.format(h) + ":" + df.format(m)
    + ":" + df.format(s));
revalidate();
repaint();

Edit 编辑

Based on your new requirements, 根据您的新要求,

Replace 更换

final long startTime = System.currentTimeMillis();

with

final Calendar startTime = Calendar.getInstance();

and then 接着

DecimalFormat df = new DecimalFormat("00");
long endTime = System.currentTimeMillis();
long diff = endTime - startTime.getTimeInMillis();
int h = (int) (diff) / (60 * 60 * 1000);
diff -= h * (60 * 60 * 1000);
int m = (int) (endTime - startTime.getTimeInMillis()) / (60 * 1000);
diff -= m * (60 * 1000);
int s = (int) (diff / 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM