大牛们 过来看看啊、、、、、

yiliang1990 yiliang1990 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这个程序怎么不能显示出 那个正方形和矩形。。。。。 求求大神们 帮帮忙啊


import java.awt.*;
import javax.swing.*;

public class ThreadTest1 extends JFrame implements Runnable {
private Thread t1, t2;
private int x1=-100;
private int x2=-50;
JPanel j=new JPanel();

public ThreadTest1(){
super("多线程1");

setSize(400,300);
setBackground(Color.WHITE);

t1=new Thread(this);
t2= new Thread(this);

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

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void paint(Graphics g){
Graphics2D comp2D=(Graphics2D)g;
comp2D.setColor(getBackground());//清除
comp2D.fillRect(0, 0, getSize().width, getSize().height);

comp2D.setColor(Color.BLUE);//矩形
comp2D.drawRect(x1, 100, 50, 50);

comp2D.setColor(Color.BLACK);// 圆
comp2D.drawOval(x2, 200, 50, 50);
}

public void run(){
while(true){
if(Thread.currentThread()==t1){
x1+=6;
if(x1>=500){
x1=-100;
repaint();
pause(100);
}else if(Thread.currentThread()==t2){
x2+=4;
if(x2>=500){
x2=-50;
repaint();
pause(120);
}
}
}
}

}
public void pause(int time){
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]){
new ThreadTest1();
}
}
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
yiliang1990
Java SE 62,621 成员 307,251 内容