关于多线程

littleking 2004-03-30 11:40:17
我要在JTextArea 里显示一句话在我按了一个button之后,怎样让他永不停止在我按另一个button之前?请高手赐教
...全文
379 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kiton 2004-03-31
  • 打赏
  • 举报
回复
何苦呢?
setText("How are you")
按下另一个按钮后
setText("")
不就ok了?
zhengoodman 2004-03-31
  • 打赏
  • 举报
回复
下面的程序稍做改动就是你想要的效果.
在下面的例子中点击start按扭线程开始工作:每隔一秒钟显示一次当前时间;
点击stop按扭后,线程就结束了生命,再点击start按扭,线程已经不能再开始工作了。

import java.awt.event.*;

import java.awt.*;

import java.util.Date;

class Example19_11 extends Frame implements Runnable, ActionListener{

Thread thread=null;

TextArea text=null;

Button b_start=new Button("Start"), b_stop=new Button("Stop");



Example19_11(){

thread = new Thread(this);

text=new TextArea();

add(text,"Center");

Panel p=new Panel();

p.add(b_start);

p.add(b_stop);

b_start.addActionListener(this);

b_stop.addActionListener(this) ;

add(p,"North");

setVisible(true);

setSize(500,300);

pack();

setResizable(false); //让窗口的大小不能被调整。

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}



public void actionPerformed(ActionEvent e){

if(e.getSource()==b_start){

try {

thread.start();

} catch(Exception e1){

text.setText("在线程没有结束run方法之前,不赞成让线程再调用start方法");

}

} else if(e.getSource()==b_stop){

thread.interrupt(); //会抛出InterruptedException

}

}

public void run() {

while(true){

text.append("\n"+new Date());

try{

thread.sleep(1000);

}catch(InterruptedException ee){

text.setText("我被消灭");

return; //结束run语句,消灭该线程。

}

}

}

public static void main(String args[]){

Example19_11 tt=new Example19_11();

}

}

zhengoodman 2004-03-30
  • 打赏
  • 举报
回复
先mark,分太低,值不值得做?
runki 2004-03-30
  • 打赏
  • 举报
回复
虽然不太懂,也看不明白!!关注。。。。
littleking 2004-03-30
  • 打赏
  • 举报
回复
抱歉,我说的不太明白,就是我想让一句话比如"how are you",重复显示一万遍,甚至更多,但是在我不中断它之前,就让"how are you"这句话永远重复显示下去。这样,应该怎样做?
Iforgot 2004-03-30
  • 打赏
  • 举报
回复
和多线程无关。 直接setText就可以了。

不按另一个不重设。按了后重设就可以了。
sunny9521 2004-03-30
  • 打赏
  • 举报
回复
虽然不太懂,也看不明白!!关注。。。。
centralplains 2004-03-30
  • 打赏
  • 举报
回复
怎么你是外国人?说话前言不答后语的.
zhugang 2004-03-30
  • 打赏
  • 举报
回复
用TimerTask
fantasyCoder 2004-03-30
  • 打赏
  • 举报
回复
在一个thread的run方法
用一个boolean的flag为标记
while(flag){

...


}
在令一个thread里控制这个flag!

62,623

社区成员

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

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