qq_34228570
CavalryOuO
2017-05-10 12:49

java多线程画图,界面空白

20
  • swing
  • 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条回答

为你推荐