Java Swing Timer Countdown

后端 未结 2 706
梦谈多话
梦谈多话 2020-11-30 16:17

I have to make a countdown program which also shows the tenths of a second; for example from a 10.0 second countdown, it should display 9.9s, 9.8s, ... 0.0s

         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 16:46

    Updating the label probably takes more that 1ms, which is why it can't keep up. If you only need to display tenths of a second, simply have your timer update less often.

    ActionListener countDown=new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            timeLeft -= 100;
            SimpleDateFormat df=new SimpleDateFormat("mm:ss:S");
            jLabel1.setText(df.format(timeLeft));
            if(timeLeft<=0)
            {
                timer.stop();
            }
        }
    };
    Timer timer=new Timer(100, countdown);
    

提交回复
热议问题