你的这个程序关系到JAVA中线程的概念!
程序并没有死机!那是因为你设定了延时为一秒钟,也就是说你的那个程序要过一分钟后才能看到结果,并且结果是会在t1中直接显示60,它并不能实现动态改变的效果!
你新建一个线程,将你的for循环的内容放到新建线程的run()方法中,然后,在go()中调用线程实例的.start()方法!
就行啦!
你可以参考Thread这个类的API文档

解决方案 »

  1.   

    你的go方法在运行,只是需要等60秒钟而已import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Frame1 extends JFrame implements Runnable {
      JTextField t1 = new JTextField(10);
      JToggleButton c1 = new JToggleButton("Start");
      int i;  public Frame1(){
      
         Container cp=getContentPane();
         cp.setLayout(new FlowLayout());
         cp.add(t1);
         cp.add(c1);
         c1.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){

    Thread t = new Thread(Frame1.this);
            t.start();
       }
       } );
         
      }  public void run(){
          try{
            for( i=1;i<61;i++)
            {
              t1.setText(String.valueOf(i));
      Thread.sleep(100);
            }
          }catch(Exception e1){e1.printStackTrace();}
      }  
        
      public static void main(String[] args){
        Frame1 frame1 = new Frame1();
        frame1.setSize(300,300);    frame1.addWindowListener(new java.awt.event.WindowAdapter() { 
    public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0);}});    frame1.show();
      }
    }
      

  2.   

    这样是可以,但是只要连击开始键几下显示就会变快,
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Frame1 extends JFrame implements Runnable
    {
      JTextField t1 = new JTextField(10);
      JToggleButton c1 = new JToggleButton("开始");
      int i;  public Frame1(){  Container cp=getContentPane();
     cp.setLayout(new FlowLayout());
     cp.add(t1);
     cp.add(c1);
         c1.addActionListener(new ActionListener()
          {
    public void actionPerformed(ActionEvent e)
    {
    Thread t = new Thread(Frame1.this);
    t.start();
    }
    });  }  public void run(){
          try{
            for( i=1;i<61;i++)
            {
              Thread.sleep(1000);
              t1.setText(String.valueOf(i));        }
          }catch(Exception e1){e1.printStackTrace();}
      }/*  class Bstart  implements ActionListener{
       public void actionPerformed(ActionEvent e){
       go();   }
       }
    */  public static void main(String[] args){
        Frame1 frame1 = new Frame1();
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setSize(300,300);
        frame1.show();
      }
    }