用Thread.currentThread().sleep(1000)
试试看

解决方案 »

  1.   

    偶的代码太长了,8000多行,呵呵,有些是机密,不好意思。我贴点相关的吧,就是按动一个按钮,在文本里显示倒计时。
    public void ShowTime(){
    int min=Integer.parseInt(thetime);while(min>0)
      {
       
    try{Thread.sleep(6000);}
    catch(InterruptedException ee){System.out.println("erro"+ee);}
    min=min-1;
    Stime.setText(String.valueOf(min));
      }
    }
    这是在init中的(节选)Stime=new JTextField(20); JButton Ok = new JButton("試験開始"); 
    Ok.setBackground(hyel); 
    Ok.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent ae){ 
    if(flg==0) 

    CenterPanel.setVisible(true); 
    ShowTime();flg=1; 
    }
    else 
    。 

    });  运行的时候,在文本理不显示时间,只有全运行完以后才显示为0!郁闷,不知应再怎么写!
     
     
      

  2.   

    不是线程的问题
    是你没有刷新控件的显示
    调用整个 CenterPanel.validate()或者CenterPanel.repaint()
    方法试试
      

  3.   

    public void ShowTime(){
    int min=Integer.parseInt(thetime); while(min>45)
      {
      
    try{
    Stime.setText(String.valueOf(min));
    CenterPanel.repaint();
    Thread.currentThread().sleep(1000);
    }
    catch(InterruptedException ee){System.out.println("erro"+ee);}
    min=min-1;

      }
    }我这么做的,也不好用!
      

  4.   

    各位能否给我代码?分不是问题的,我比较郁闷,公司没人做java,我又没做过呀,唉!这个好像不是很难的,可能是我其他的地方有问题,大家可以写个通的代码吗?不胜感谢!
      

  5.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;public class a extends JFrame{
    private JButton btn_start = new JButton("Start");
    private JLabel lbl_time = new JLabel("??婍");
    private int min = 10;

    public a()
    {
    setSize(400,300);
    getContentPane().add(btn_start,BorderLayout.NORTH);
    getContentPane().add(lbl_time,BorderLayout.CENTER);
    btn_start.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    Thread runner = new Thread()
    {
    public void run()
    {
    showTime();
    }
    };
    runner.start();
    }
    });
    }

    public static void main(String agrs[]){
    a test = new a();
    test.show();
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void showTime()
    {
    while(min>0)
      {
        try
        {
        Thread.sleep(1000);
        }
    catch(InterruptedException ee)
    {
    System.out.println("erro"+ee);
    }
    min=min-1;
    lbl_time.setText(String.valueOf(min));
    getContentPane().validate();
       }
    }
    }