java中的动态时钟

jt1*_*153 8 java swing clock

我希望在我的程序中实现一个时钟,以便在程序运行时显示日期和时间.我已经研究了getCurrentTime()方法和Timers,但它们似乎没有做我想做的事情.

问题是我可以在程序加载时得到当前时间,但它永远不会更新.任何有待观察的建议都将不胜感激!

jjn*_*guy 14

你需要做的是使用Swing的Timer课程.

让它每秒运行一次,并用当前时间更新时钟.

Timer t = new Timer(1000, updateClockAction);
t.start();
Run Code Online (Sandbox Code Playgroud)

这将导致updateClockAction每秒发射一次.它将在EDT上运行.

您可以进行updateClockAction类似以下的操作:

ActionListener updateClockAction = new ActionListener() {
  public void actionPerformed(ActionEvent e) {
      // Assumes clock is a custom component
      yourClock.setTime(System.currentTimeMillis()); 
      // OR
      // Assumes clock is a JLabel
      yourClock.setText(new Date().toString()); 
    }
}
Run Code Online (Sandbox Code Playgroud)

因为这会每秒更新一次时钟,所以在更糟糕的情况下,时钟会关闭999ms.要将此情况增加到更糟的99ms的错误边际,您可以增加更新频率:

Timer t = new Timer(100, updateClockAction);
Run Code Online (Sandbox Code Playgroud)

  • 我做了一个测试,使用 javax.swing.Timer 更新总是落后大约半秒,但我不知道为什么 (2认同)

Osc*_*Ryz 5

您必须每秒在单独的线程中更新文本.

理想情况下,你应该只在EDT(事件调度程序线程)中更新swing组件,但是在我在我的机器上尝试之后,使用Timer.scheduleAtFixRate给了我更好的结果:

java.util.Timer http://img175.imageshack.us/img175/8876/capturadepantalla201006o.png

javax.swing.Timer版本总是落后大约半秒:

javax.swing.Timer http://img241.imageshack.us/img241/2599/capturadepantalla201006.png

我真的不知道为什么.

这是完整的来源:

package clock;

import javax.swing.*;
import java.util.*;
import java.text.SimpleDateFormat;

class Clock {
    private final JLabel time = new JLabel();
    private final SimpleDateFormat sdf  = new SimpleDateFormat("hh:mm");
    private int   currentSecond;
    private Calendar calendar;

    public static void main( String [] args ) {
        JFrame frame = new JFrame();
        Clock clock = new Clock();
        frame.add( clock.time );
        frame.pack();
        frame.setVisible( true );
        clock.start();
    }
    private void reset(){
        calendar = Calendar.getInstance();
        currentSecond = calendar.get(Calendar.SECOND);
    }
    public void start(){
        reset();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate( new TimerTask(){
            public void run(){
                if( currentSecond == 60 ) {
                    reset();
                }
                time.setText( String.format("%s:%02d", sdf.format(calendar.getTime()), currentSecond ));
                currentSecond++;
            }
        }, 0, 1000 );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是使用javax.swing.Timer修改的源代码

    public void start(){
        reset();
        Timer timer = new Timer(1000, new ActionListener(){
        public void actionPerformed( ActionEvent e ) {
                if( currentSecond == 60 ) {
                    reset();
                }
                time.setText( String.format("%s:%02d", sdf.format(calendar.getTime()), currentSecond ));
                currentSecond++;
            }
        });
        timer.start();
    }
Run Code Online (Sandbox Code Playgroud)

可能我应该改变计算日期的字符串的方式,但我认为这不是问题所在

我已经读过,因为Java 5推荐的是:ScheduledExecutorService我给你留下了实现它的任务.