相关疑难解决方法(0)

使用SwingWorker和Timer在标签上显示时间?

我希望有一个时钟显示当前时间并每秒刷新一次.我使用的代码是:

int timeDelay = 1000;
ActionListener time;
time = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            timeLabel.setText(DateTimeUtil.getTime()); 
            /*timeLabel is a JLabel to display time,
            getTime() is samll static methos to return formatted String of current time */
        }
    };
SwingWorker timeWorker = new SwingWorker() {

        @Override
        protected Object doInBackground() throws Exception {

            new Timer(timeDelay, time).start();
            return null;
        }
    };
timeWorker.execute();
Run Code Online (Sandbox Code Playgroud)

我想timeLabel在EDT以外的其他线程中刷新文本.
我做得对吗?还有其他更好的办法?
另外,对于信息,我已添加timeLabel到包含几个类似实用程序的a中,并在另一个中调用.extendedJPanelMainJFrame

java swing swingworker javax.swing.timer

5
推荐指数
1
解决办法
5024
查看次数

标签 统计

java ×1

javax.swing.timer ×1

swing ×1

swingworker ×1