为什么我的run()不运行

hu1dao 2003-08-20 02:46:13
package test20;
import javax.microedition.lcdui.*;
public class Displayable3 extends Canvas implements CommandListener,Runnable {
int i=0;
public void run() {
i++;
repaint();
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}

public Displayable3() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
setCommandListener(this);
addCommand(new Command("Exit", Command.EXIT, 1));
run();
}

public void commandAction(Command command, Displayable displayable) {
}

protected void paint(Graphics g) {
System.out.println(i);
}


}
结果是系统输出一个1就什么都没有了
...全文
44 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hu1dao 2003-08-20
  • 打赏
  • 举报
回复
to UFOFox(风) ( )
真不知道怎么感谢你好!
UFOFox 2003-08-20
  • 打赏
  • 举报
回复
你的用法有误,线程不是一个run()函数这么简单,它是一种机制你可以这样做:
//...implements Runnable{
//...
private Thread thread;
//构造函数(){
thread = new Thread(this);
thread.start();
}
public void run(){
int i =0;
while(true){//must use loop if you want to test i
try{
thread.sleep(//Your did);

}
catch(InterruptedException ex){

}

System.out.println(""+i++);
}
}

}
hisi 2003-08-20
  • 打赏
  • 举报
回复
你想要怎么样??

13,100

社区成员

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

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