Java定时器,每秒更新一次

Java定时器,每秒更新一次,java,swing,date,time,timer,Java,Swing,Date,Time,Timer,我想从系统中获取当前日期和时间,我可以使用以下代码: private void GetCurrentDateTimeActionPerformed(java.awt.event.ActionEvent evt) { DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date

我想从系统中获取当前日期和时间,我可以使用以下代码:

    private void GetCurrentDateTimeActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date date = new Date();
    CurrentDateTime.setText(dateandtime.format(date));
}                                                  
这样做很好,因为它会抓取当前日期和时间没有问题,但是它不是动态的,因为时间不会更新,除非再次按下按钮。因此,我想知道如何通过每秒更新函数来刷新时间,从而使此按钮更具动态性。

您可以使用定期更新。大概是这样的:

ScheduledExecutorService e= Executors.newSingleThreadScheduledExecutor();
e.scheduleAtFixedRate(new Runnable() {
  @Override
  public void run() {
    // do stuff
    SwingUtilities.invokeLater(new Runnable() {
       // of course, you could improve this by moving dateformat variable elsewhere
       DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       Date date = new Date();
       CurrentDateTime.setText(dateandtime.format(date));
    });
  }
}, 0, 1, TimeUnit.SECONDS);
您可以使用定期更新。大概是这样的:

ScheduledExecutorService e= Executors.newSingleThreadScheduledExecutor();
e.scheduleAtFixedRate(new Runnable() {
  @Override
  public void run() {
    // do stuff
    SwingUtilities.invokeLater(new Runnable() {
       // of course, you could improve this by moving dateformat variable elsewhere
       DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       Date date = new Date();
       CurrentDateTime.setText(dateandtime.format(date));
    });
  }
}, 0, 1, TimeUnit.SECONDS);
Swing计时器(javax.Swing.timer的实例)在指定的延迟后触发一个或多个操作事件。 参考:

Swing计时器(javax.Swing.timer的实例)在指定延迟后触发一个或多个操作事件。 参考:

为此使用摆动计时器:

DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Timer t = new Timer(500, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Date date = new Date();
        CurrentDateTime.setText(dateandtime.format(date));
        repaint();
    }
});
t.start();

为此使用Swing Timer:

DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Timer t = new Timer(500, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Date date = new Date();
        CurrentDateTime.setText(dateandtime.format(date));
        repaint();
    }
});
t.start();

首先定义一个TimerTask

class MyTimerTask extends TimerTask  {
    JLabel currentDateTime;

     public MyTimerTask(JLabel aLabel) {
         this.currentDateTime = aLabel;
     }

     @Override
     public void run() {
         SwingUtilities.invokeLater(
                 new Runnable() {

                    public void run() {
                        // You can do anything you want with 'aLabel'
                         DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                         Date date = new Date();
                         currentDateTime.setText(dateandtime.format(date));

                    }
                });
     }
}
然后,您需要在应用程序或UI启动时创建java.util.Timer。例如,您的main()方法


首先定义一个TimerTask

class MyTimerTask extends TimerTask  {
    JLabel currentDateTime;

     public MyTimerTask(JLabel aLabel) {
         this.currentDateTime = aLabel;
     }

     @Override
     public void run() {
         SwingUtilities.invokeLater(
                 new Runnable() {

                    public void run() {
                        // You can do anything you want with 'aLabel'
                         DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                         Date date = new Date();
                         currentDateTime.setText(dateandtime.format(date));

                    }
                });
     }
}
然后,您需要在应用程序或UI启动时创建java.util.Timer。例如,您的main()方法


@jbniset
Timer
实际上在其大多数实现中都有一个“缺陷”(请参见)@fge:这个问题是关于java.util.Timer的,而不是javax.swing.Timer的。在我的javax.swing.Timer实现中,使用了nanoTime。@jbniset
Timer
在其大多数实现中实际上都有一个“缺陷”(请参阅)@fge:这个问题是关于java.util.Timer的,而不是关于javax.swing.Timer的。在我的javax.swing.Timer实现中,使用了nanoTime;它从计时器的线程访问
JLabel
。@MKAftab垃圾神的评论是正确的;这个示例代码错误得很危险。在后台线程上操作Swing组件将导致奇怪的行为并可能失败。好好学习。要么修改此代码,要么收回答案。@Basil您的答案是正确的。我编辑了我的答案。现在每件事都要通过EDT。但现在,我认为,使用Swing定时器是一个更方便的解决方案。看起来不错。现在,将java.util.Date和SimpleDataFormat的使用替换为,这将是完美的这是不正确的同步;它从计时器的线程访问
JLabel
。@MKAftab垃圾神的评论是正确的;这个示例代码错误得很危险。在后台线程上操作Swing组件将导致奇怪的行为并可能失败。好好学习。要么修改此代码,要么收回答案。@Basil您的答案是正确的。我编辑了我的答案。现在每件事都要通过EDT。但现在,我认为,使用Swing定时器是一个更方便的解决方案。看起来不错。现在,将java.util.Date和SimpleDataFormat的使用替换为,这将是完美的谢谢你的帮助!这是完美的;不过,为了帮助其他人,应该调用@Override,以便代码能够正常运行:)谢谢您的帮助!这是完美的;但是,为了帮助其他人,应该调用@Override,以便代码能够正常运行:)