日期:2014-05-20  浏览次数:20755 次

定时器中,定义的任务是每10秒弹出一个窗口,窗口停留3秒后自动消失。
代码如下:

 package kankan;                          
 import java.util.*;                      
 import javax.swing.*;                    
import java.awt.event.*;                  
import java.awt.*;                        
import javax.swing.Timer;                 
//判断是否在线                            
public class CloseWindowIn5 extends JFrame{                                                  
private JButton button=new JButton("签到")                          
public void  window(){                                                            
 this.setSize(300, 400);                  
 this.setLocation(40, 50);                
 this.setVisible(true);                   
 setLayout(null);                         
 button.setBounds(110,180,70,70);         
 add(button);                             
 button.addActionListener(new ActionListen
  public void actionPerformed(ActionEvent 
  System.out.println(“在线”);                               
  }                                       
});                                       
 }                                                                                   
public void run() {                       
try {                                     
Thread.sleep(5000);                       
 this.dispose();                          
} catch (InterruptedException e) {        
e.printStackTrace();                      
}                                                                                  
}