如何在Java Swing中显示更改的文本(即,显示时间与更改秒数) [英] How to display changing text in Java Swing (ie, display time with changing seconds)

查看:91
本文介绍了如何在Java Swing中显示更改的文本(即,显示时间与更改秒数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在JFrame上显示当前日期和时间作为JLabel的方法,但是会自动更新。一旦绘制到窗格中,我不需要担心手动更新它,因为它将每2秒刷新一次。

I'm looking for a way to display the current date and time on a JFrame as a JLabel but have it update automagically. Once it is drawn to the pane, I shouldn't need to worry about manually updating it because it will refresh say, every 2 seconds.

是否有正确的方法这样做而不是每2秒手动更改JLabel的文本值。

Is there a proper way of doing this? Instead of manually changing the JLabel's text value every 2 seconds.

推荐答案

使用 javax.swing.Timer 以确保更新Swing零件发生在EDT上。

Use the javax.swing.Timer to ensure that updates to the Swing component occur on the EDT.

Timer t = new javax.swing.Timer(2000, new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        yourLabel.setText(...);
    }
});
t.start();

这篇关于如何在Java Swing中显示更改的文本(即,显示时间与更改秒数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆