我编写了一个多线程的小应用程序:实现上下两行字符的左右来回移动。
但程序运行时没有任何效果!
程序如下:
import java.awt.*;
import java.applet.*;public class ThreadString extends Applet
{
public void init()
{
Thread thr1=new Thread(new Thread1());
Thread thr2=new Thread(new Thread2());
thr1.start();
thr2.start();
}
}
class Thread1 extends Canvas
implements Runnable
{ int y1;
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{
if(y1<0) y1=200; else y1=y1-10;
g.drawString("hello,java!",y1,20);
}
}
class Thread2 extends Canvas
implements Runnable
{
int y2;
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{
if(y2>200) y2=0; else y2=y2+10;
g.drawString("hello,java!",y2,40);
}
}
但程序运行时没有任何效果!
程序如下:
import java.awt.*;
import java.applet.*;public class ThreadString extends Applet
{
public void init()
{
Thread thr1=new Thread(new Thread1());
Thread thr2=new Thread(new Thread2());
thr1.start();
thr2.start();
}
}
class Thread1 extends Canvas
implements Runnable
{ int y1;
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{
if(y1<0) y1=200; else y1=y1-10;
g.drawString("hello,java!",y1,20);
}
}
class Thread2 extends Canvas
implements Runnable
{
int y2;
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{
if(y2>200) y2=0; else y2=y2+10;
g.drawString("hello,java!",y2,40);
}
}
解决方案 »
- Java怎样把一个十六进制的字符串数组存到Byte数组中
- Swing JTable中checkBox的问题 谁能帮帮我?
- keypress中按下键没反应
- 如何用脚本实现jdk的安装
- 如何实现浏览器的收藏夹功能?????
- 请问高手们,JAVA(状态栏类)有没有这个类啊,小弟急啊,先谢了!
- 100分选赏>>如何实现JAVA数据库的即时刷新!!!
- 有谁知道 Sun ONE Studio 4 update1 怎么生成 .exe 文件?
- 哪有for ms sqlserver的java驱动?好想得到他呀
- 怎样实现java程序的单步调试?
- 有谁能给一个比较详细点的十字链表java版的演示代码
- 求一软件工程管理工具(比对两个.java代码有何不同)
import java.applet.*;public class ThreadString extends Applet
{
public void init()
{
Canvas t1=new Thread1(); //实例化画布的同时启动线程
this.add(t1);
Canvas t2=new Thread2();
this.add(t2);
}
}
class Thread1 extends Canvas
implements Runnable
{ int y1;
public Thread1() //添加了构造,用于设定画布大小,并启动线程
{
this.setSize(300,150);
new Thread(this).start();
}
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{
if(y1<0) y1=200; else y1=y1-10;
g.drawString("hello,java!",y1,20);
}
}
class Thread2 extends Canvas
implements Runnable
{
int y2;
public Thread2()
{
this.setSize(300,150);
new Thread(this).start();
}
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{
if(y2>200) y2=0; else y2=y2+10;
g.drawString("hello,java!",y2,40);
}
}