import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class dis extends JFrame implements Runnable
{
JTextField t;
JButton b1;
JButton b2;
Thread th;
public boolean runFlag = true;
int count = 0;
public dis()
{
try
{
b1=new JButton();
b2=new JButton();
b1.addActionListener(new on());
b2.addActionListener(new off());
b1.setText("START");
b2.setText("STOP");
t=new JTextField(10);
th=new Thread();

this.getContentPane().setLayout(null);
t.setSize(130,30);
b1.setBounds(2,50,80,20);
b2.setBounds(89,50,70,20);
getContentPane().add(t);
getContentPane().add(b1);
getContentPane().add(b2);
this.setBounds(100,100,190,130);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void run() 
{
     for(int a=1;;a++)
     {
       try {
         if(runFlag==true) 
         {
         t.setText(Integer.toString(a));
         }
         th.sleep(100);
       } 
       catch (Exception t)
       {
       System.out.println(t.getMessage());
       }
      
     }
   }
class on implements ActionListener 
{
    public void actionPerformed(ActionEvent e) 
    {
     new Thread(dis.this).start();
    }
}
class off implements ActionListener 
{
    public void actionPerformed(ActionEvent e) 
    {
      runFlag=false;
    } 
}
}
public class Test1
{
public static void main(String args[])
{
new dis();
}
}
修改了一下,可以勉强实现,还需修改!

解决方案 »

  1.   

    你这个根本就不是线程嘛。你要另外写一个class MyThread extends Thread, 在那里面做go的事情才行的
      

  2.   

    这样改行不行?
    我没试
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    class dis extends JFrame implements Runnable
    {
    JTextField t;
    JButton b1;
    JButton b2;
    Thread th;
    public boolean runFlag = true;
    int count = 0;
    public dis()
    {
    try
    {
    b1=new JButton();
    b2=new JButton();
    b1.addActionListener(new on());
    b2.addActionListener(new off());
    b1.setText("START");
    b2.setText("STOP");
    t=new JTextField(10);
    th=new Thread();
    th.start();
    this.getContentPane().setLayout(null);
    t.setSize(130,30);
    b1.setBounds(2,50,80,20);
    b2.setBounds(89,50,70,20);
    getContentPane().add(t);
    getContentPane().add(b1);
    getContentPane().add(b2);
    this.setBounds(100,100,190,130);
    this.setVisible(true);
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }
    }
    public void run() 
    {
         for(int a=1;;a++)
         {
           try {
             if(runFlag==true) 
             {
             t.setText(Integer.toString(a));
             }
             th.sleep(100);
           } 
           catch (Exception t)
           {
           System.out.println(t.getMessage());
           }
          
         }
       }
    class on implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
         new Thread(this).start();
        }
    }
    class off implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
                   if(runflag=true)
          runFlag=!runFlag;
        } 
    }
    }
    public class mythread
    {
    public static void main(String args[])
    {
    new dis();
    }
    }
      

  3.   

    是呀,好乱的程序。
    既然是在Button1中加的监听就不用把Go()方法设为Public,直接写到里边就可以!你源程序的主要问题是你虽然加了按钮的事件,但在Main里边运行的只是你初始化的一部分。
    建议你先把程序的结构弄清楚点。这要不仅思路可以清楚点 ,也可以更省气!!!加油