Java-每秒重绘一次组件吗? [英] Java - repaint component every second?

查看:48
本文介绍了Java-每秒重绘一次组件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每秒钟重涂一次组件,但是没有用.我正在尝试的是:

I would like to repaint component after each second, but it didn't work. What I am trying is:

    try{
        while(true){
            Thread.currentThread().sleep(1000);
            gc.cb.next();
            gc.repaint();
        }
    }
    catch(Exception ie){
    }

推荐答案

我建议使用

I would advise using a javax.swing.Timer for this problem, which will periodically fire an ActionEvent on the Event Dispatch thread (note that you should only call repaint and / or manipulate Swing components from this thread). You can then define an ActionListener to intercept the event and repaint your component at this point.

示例

JComponent myComponent = ...
int delay = 1000; //milliseconds

ActionListener taskPerformer = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    myComponent.repaint();
  }
};

new Timer(delay, taskPerformer).start();

还要注意, SwingWorker 可能不合适,因为它通常用于具有定义的开始和结束的后台任务,而不是定期任务.

Also note that SwingWorker is probably inappropriate as it is typically used for background tasks that have a defined start and end, rather than a periodic task.

这篇关于Java-每秒重绘一次组件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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