关于多线程 和多线程无关。 直接setText就可以了。不按另一个不重设。按了后重设就可以了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 抱歉,我说的不太明白,就是我想让一句话比如"how are you",重复显示一万遍,甚至更多,但是在我不中断它之前,就让"how are you"这句话永远重复显示下去。这样,应该怎样做? 在一个thread的run方法用一个boolean的flag为标记while(flag){...}在令一个thread里控制这个flag! 下面的程序稍做改动就是你想要的效果.在下面的例子中点击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(); }} 何苦呢?setText("How are you")按下另一个按钮后setText("")不就ok了? 怎样判断目录是否是只读的? 指教一下多线程 重发:getColumnName()怎么和getColumnLabel()一样?怎么才能得到字段名? java ResultSetMetaData 问一个方法调用的问题 求助高人!! 如何在JFrame的子窗口关闭前不能操纵父窗口!!! javac的简单问题 刚学j2ee,觉得servlet,html,xml代码输入很郁闷呀? 接口问题 jpg输出问题 安装jubilder7 enterprise,在new 中怎么没有webservice选项 急!!!请问高手:在java程序中如何获得CPU、内存等系统资源利用率
用一个boolean的flag为标记
while(flag){...
}
在令一个thread里控制这个flag!
在下面的例子中点击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(); }}
setText("How are you")
按下另一个按钮后
setText("")
不就ok了?