用Java重新连续重绘 [英] Repainting Continuously in Java

查看:142
本文介绍了用Java重新连续重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用线程的Java程序。在我的run方法中,我有:

I have a Java program that uses threads. In my run method, I have:

public void run() {
    while(thread != null){
        repaint();
        System.out.println("hi");
        try {  
            Thread.sleep(1000);  
        } catch (InterruptedException e) {  
            break;  
        }  
    }
}

public void paintComponent(Graphics g) {
    // painting stuff
}

问题是执行了run方法,但 paintComponent 部分不叫。如果这不是重新绘制组件的正确方法,那么我应该如何重新绘制它?

The problem is that the run method is executed, but the paintComponent section is not called. If this is not the right way to keep repainting the component, then how should I repaint it?

推荐答案

你必须叫油漆(g)重型容器,例如JFrame。您可以将paintComponent(g)称为轻量级容器(如JButton)。看看是否有效。

You have to call paint(g) for a heavy-weight container such as a JFrame. You call paintComponent(g) for light-weight containers like a JButton. See if that works.

这篇关于用Java重新连续重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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