定时器与回调

测试类

import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Test {

    public static void main(String[] args) {
        ActionListener listen=new TimeToPrint();
        Timer t=new Timer(10,listen);
        t.start();

        JOptionPane.showMessageDialog(null, "Quit progran?");
        System.exit(0);
    }

}

实现接口

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class TimeToPrint implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        Date now=new Date();
         System.out.println("Now the time is "+now);
         Toolkit.getDefaultToolkit().beep();

    }




}

你可能感兴趣的:(定时器与回调)