类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
程序1:(不使用线程)
import java.awt.*;
public class graphtest extends Frame
{
public static void main(String[] args)
{
Frame f=new graphtest();
f.resize(300,300);
f.setBackground(Color.cyan);
f.show();
}
public void paint(Graphics g)
{
g.setColor(Color.black);
drawcircle t=new drawcircle(g);
t.draw();
}
}
class drawcircle
{
Graphics bg;
public drawcircle(Graphics g)
{
bg=g;
}
public void draw()
{
bg.drawArc(50,50,100,100,0,360);
}
}
程序2:(使用线程)
import java.awt.*;
public class graphtest2 extends Frame
{
public static void main(String[] args)
{
Frame f=new graphtest2();
f.resize(300,300);
f.setBackground(Color.cyan);
f.show();
}
public void paint(Graphics g)
{
g.setColor(Color.black);
drawcircle t=new drawcircle(g);
t.start();
}
}
class drawcircle extends Thread
{
Graphics bg;
public drawcircle(Graphics g)
{
bg=g;
}
public void run()
{
bg.drawArc(50,50,100,100,0,360);
}
}
程序1(不使用线程)可以画出一个圆,程序2(使用线程)画不出来,这是为什么?
网友回答:
修改代码如下,可以和原来一样
public void paint(Graphics g) {
g.setColor(Color.black);
drawcircle t = new drawcircle(g);
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}