try{
Thread.sleep(5000)
}catch(Exception any){}

解决方案 »

  1.   

    try{
    Thread.sleep(5000)
    }catch(Exception any){}这是线程的休眠方法,对于非线程管用吗?
      

  2.   

    even you never create a new thread explictly, there is always a thread: you current running thread..(if there is no thread, how can your program got run?). So sleep is working here.
      

  3.   

    刚写的,贴上。 用线程sleep(1000)实现的。这种方法不是很好。因为sleep(1000)不是很精确。
    显示时间的frame。***********************************import javax.swing.*;
    import java.awt.*;
    import java.util.Date;
    import java.awt.event.*;
    import com.borland.jbcl.layout.*;
    public  class DispDate extends JFrame{
      private JButton exitButton = new JButton();
      private BorderLayout borderLayout1 = new BorderLayout();
      private JLabel label = new JLabel();
      DispDate() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public void setLabel(String str){
        label.setText(str);
      }
      private void jbInit() throws Exception {
        label.setText(new Date().toString());
        this.getContentPane().add(label, BorderLayout.NORTH);
        this.getContentPane().add(exitButton,  BorderLayout.SOUTH);
        exitButton.setText("退出");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            exitButton_actionPerformed(e);
          }
        });
      }  void exitButton_actionPerformed(ActionEvent e) {
        System.exit(21);  }
      public static void main(String[] args) throws Exception{
         try {
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         }
         catch(Exception e) {
           e.printStackTrace();
         }
         DispDate ct=new DispDate();
         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
         Dimension frameSize = ct.getSize();
         if (frameSize.height > screenSize.height) {
           frameSize.height = screenSize.height;
         }
         if (frameSize.width > screenSize.width) {
           frameSize.width = screenSize.width;
         }
         ct.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
         ct.setVisible(true);
         ct.setTitle("时钟") ;     ct.pack();     MyThread a=new MyThread();
         a.start();
         for(int i=0;true;i++){
           a.sleep(1000);
           System.out.println(new Date().toString());
           ct.setLabel(new Date().toString());
         }
      }
    }
    class MyThread extends Thread{
      public  void  run()  {
      }
    }
      

  4.   

    你ke可以用java.util.Timer 来实现延迟:
    public class AnnoyingBeep { T
     oolkit toolkit; 
     Timer timer; 
     public AnnoyingBeep() { 
      toolkit = Toolkit.getDefaultToolkit(); 
      timer = new Timer(); 
      timer.schedule(new RemindTask(), 0, //initial delay 1*1000); //subsequent   rate 
     } class RemindTask extends TimerTask { 
      int numWarningBeeps = 3; 
      public void run() { 
       if (numWarningBeeps > 0) {
        toolkit.beep(); 
        System.out.println("Beep!"); 
        numWarningBeeps--; 
       } 
       else { 
        toolkit.beep(); 
        System.out.println("Time´s up!"); 
        //timer.cancel(); //Not necessary because we call       System.exit System.exit(0); 
        //Stops the AWT thread (and everything else) 
       } 
      } 
     } 
     ...
      

  5.   

    当然可以啦。这是一个静态方法,是让当前进程sleep
      

  6.   


    public class AnnoyingBeep { 
    Toolkit toolkit; 
     Timer timer; 
     public AnnoyingBeep() { 
      toolkit = Toolkit.getDefaultToolkit(); 
      timer = new Timer(); 
      timer.schedule(new RemindTask(), 0, //initial delay 1*1000); //subsequent   rate 
     } class RemindTask extends TimerTask { 
      int numWarningBeeps = 3; 
      public void run() { 
       if (numWarningBeeps > 0) {
        toolkit.beep(); 
        System.out.println("Beep!"); 
        numWarningBeeps--; 
       } 
       else { 
        toolkit.beep(); 
        System.out.println("Time´s up!"); 
        //timer.cancel(); //Not necessary because we call       System.exit System.exit(0); 
        //Stops the AWT thread (and everything else) 
       } 
      } 
     } 
     ...
    } TimerTask是什么,系统类库吗?class RemindTask 是干什么的,这两个类好像没有联系:谁也没实例化谁的对象?
    真正起作用的是不是这一句:
     timer.schedule(new RemindTask(), 0, //initial delay 1*1000);
    我不太明白,不过还是谢谢,希望再次得到响应,多谢!  
      

  7.   

    另外,用
    try{
    Thread.sleep(5000)
    }catch(Exception any){}
    达到延时效果了,但还是整体延时,不是我希望的推迟后边语句的执行。怎么办?
      

  8.   

    下面是一个VB中的调用的API例子``不知道JAVA里能不能调用这样的API呢````
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    ------------------------------------
    Dim LongTick As Long
    LongTick = GetTickCount
    While GetTickCount - LongTick < 1000             '延时1秒
    Wend