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

yiliang1990 2011-12-16 12:56:46
这个程序怎么不能显示出 那个正方形和矩形。。。。。 求求大神们 帮帮忙啊


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();
}
}
...全文
115 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
calw1958 2011-12-16
  • 打赏
  • 举报
回复
楼下解释,搞不懂
yiliang1990 2011-12-16
  • 打赏
  • 举报
回复
但是 我还有个问题 就是为什么 正方形 始终 没有动呢
package test;

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;


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

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

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

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

repaint();

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()==t2){
x2+=4;
if(x2>=500)
x2=-50;
repaint();
pause(120);
}else
if(Thread.currentThread()==t1){
x1=+6;
if(x1>=500)
x1=-100;
repaint();
pause(100);
}
}



}
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[]){

ThreadTest1 test=new ThreadTest1();

}
}
跪求 解释
XC2010_K 2011-12-16
  • 打赏
  • 举报
回复
yiliang1990 2011-12-16
  • 打赏
  • 举报
回复
等待啊 大神们 跪求
孟祥月 2011-12-16
  • 打赏
  • 举报
回复
那就结贴吧
yiliang1990 2011-12-16
  • 打赏
  • 举报
回复
呵呵 我自己看 明白了 哈哈哈

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧