CavalryOuO 2017-05-10 12:49 采纳率: 62.5%
浏览 1408
已采纳

java多线程画图,界面空白

尝试过使用事件分发线程,但还是空白,线程确实是运行着的。
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class test1 extends JPanel {
public void paint(Graphics g){
Thread t1=new Thread(new Runnable(){
public void run(){

    g.fillRect(50, 50, 50, 50);
    for(int i=0;;i++){
    g.setColor(Color.BLACK);
    g.fillRect(100+i*20,65,20,20);
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    g.setColor(Color.WHITE);
    g.fillRect(100+i*20,65,20,20);
    if((100+(i+1)*20)>=1500){
        System.out.println("over1");
        break;
    }

    }
}});
    Thread t2=new Thread(){
    public void run(){

    g.fillRect(50, 500, 50, 50);
    for(int i=0;;i++){
    g.setColor(Color.BLACK);
    g.fillRect(100+i*20,515,20,20);
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    g.setColor(Color.WHITE);
    g.fillRect(100+i*20,515,20,20);
    if((100+(i+1)*20)>=1500){
        System.out.println("over2");
        break;
    }

    }
}};
t1.start();
t2.start();

// SwingUtilities.invokeLater(t1);
// SwingUtilities.invokeLater(t2);
}
public test1() {

    setBounds(50,50,1500,1000);
    setVisible(true);
    repaint();
}
public static void main(String[] args) {

     new test1();

}

}

  • 写回答

2条回答

  • Zz_1994 2017-05-10 12:58
    关注

    应该用一个线程每100ms调用repaint(),
    另外一个线程来控制i的变化。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波