且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java Swing Timer倒计时

更新时间:2023-12-03 23:09:58

更新标签的时间可能超过1毫秒,这就是它无法跟上的原因。如果你只需要显示十分之一秒,那么只需更新你的计时器。

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);