我在窗体中想演示一个随机抽数的过程,有两个按纽开始和停止,开始后,开始循环显示,停下后就显示数据,可是老死循环,问各位高手怎么弄?import java.awt.*;
import java.awt.event.*;
public class Loop extends Frame{
Button bt1,bt2;
TextField tf1;
String[] s1;
int k;
int flag;
class Startevent implements ActionListener{
public void actionPerformed(ActionEvent e){
flag=1;
                        loop1();
}
}
class Endevent implements ActionListener{
public void actionPerformed(ActionEvent e){
flag=0;
}
}
public void loop1(){
while(true){
for(int i=0;i<s1.length;i++){
tf1.setText(s1[i]);
k=i;
if(flag==0){break;}
}
if(flag==0){break;}
}
}
public Loop(){
setTitle("循环");
s1=new String[10];
for(int i=0;i<s1.length;i++){
s1[i]="第"+i+"个数";
}
bt1=new Button("开始");
bt2=new Button("停止");
tf1=new TextField(20);
Panel p =new Panel();
p.add(bt1);
p.add(bt2);
this.setLayout(new GridLayout(2,1));
this.add(tf1);
this.add(p);
Startevent se=new Startevent();
Endevent se2=new Endevent();

bt1.addActionListener(se);
bt2.addActionListener(se2);

}
public static void main(String[] args){
Loop fr1=new Loop();
Winevent w1=new Winevent();
fr1.addWindowListener(w1);
fr1.setSize(500,500);
fr1.setVisible(true);
}
}
class Winevent implements WindowListener{
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowClosed(WindowEvent e){};
public void windowOpened(WindowEvent e){};
public void windowIconified(WindowEvent e){};
public void windowDeiconified(WindowEvent e){};
public void windowActivated(WindowEvent e){};
public void windowDeactivated(WindowEvent e){};
}

解决方案 »

  1.   

    我想看的清楚下。。import   java.awt.*; 
    import   java.awt.event.*; 
    public   class   Loop   extends   Frame{ 
    Button   bt1,bt2; 
    TextField   tf1; 
    String[]   s1; 
    int   k; 
    int   flag; 
      class   Startevent   implements   ActionListener{ 
       public void actionPerformed(ActionEvent   e)
        { 
            flag=1; loop1(); 
        } 

      class   Endevent   implements   ActionListener{ 
       public   void   actionPerformed(ActionEvent   e)
        { 
            flag=0; 
        } 

    public void loop1(){ 
      while(true)
     { 
      for(int   i=0;i <s1.length;i++)
      { 
        tf1.setText(s1[i]); 
        k=i; 
        if(flag==0){break;} 
       } 
        if(flag==0){break;} 
      } 

    public   Loop(){ 
    setTitle( "循环 "); 
    s1=new   String[10]; 
    for(int   i=0;i <s1.length;i++){ 
    s1[i]= "第 "+i+ "个数 "; 

    bt1=new   Button( "开始 "); 
    bt2=new   Button( "停止 "); 
    tf1=new   TextField(20); 
    Panel   p   =new   Panel(); 
    p.add(bt1); 
    p.add(bt2); 
    this.setLayout(new   GridLayout(2,1)); 
    this.add(tf1); 
    this.add(p); 
    Startevent   se=new   Startevent(); 
    Endevent   se2=new   Endevent(); bt1.addActionListener(se); 
    bt2.addActionListener(se2); } 
    public   static   void   main(String[]   args){ 
    Loop   fr1=new   Loop(); 
    Winevent   w1=new   Winevent(); 
    fr1.addWindowListener(w1); 
    fr1.setSize(500,500); 
    fr1.setVisible(true); 


    class   Winevent   implements   WindowListener{ 
    public   void   windowClosing(WindowEvent   e){ 
    System.exit(1); 

    public   void   windowClosed(WindowEvent   e){}; 
    public   void   windowOpened(WindowEvent   e){}; 
    public   void   windowIconified(WindowEvent   e){}; 
    public   void   windowDeiconified(WindowEvent   e){}; 
    public   void   windowActivated(WindowEvent   e){}; 
    public   void   windowDeactivated(WindowEvent   e){}; 
    }
      

  2.   

      class   Startevent   implements   ActionListener{ 
       public void actionPerformed(ActionEvent   e)
        { 
            flag=1; loop1(); 
        } 

      class   Endevent   implements   ActionListener{ 
       public   void   actionPerformed(ActionEvent   e)
        { 
            flag=0; 
        } 

    你开始的时候把标志放到1,然后启动循环。 可是你停止的时候把标志放到0,没有启动一次循环操作去判断标志已经改为0,需要停止。
      

  3.   

    可是启动循环后,cpu占用率100%,根本按不了关闭按钮,就是加上 loop1();   也不行
      

  4.   

    老大,用线程啊,加上Thread.sleep(x)。你这样循环,CPU不100%才怪呢。