你的位置:首页 > Java教程

[Java教程]java时钟


 一般实现形式

 1 package ares.present; 2  3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.util.Date; 6  7 public class Printer implements ActionListener { 8  9   @Override10   public void actionPerformed(ActionEvent e) {11     Date date=new Date();12     System.out.println(date);13   }14 }

 1 package ares.present; 2  3 import java.awt.event.ActionListener; 4 import javax.swing.JOptionPane; 5 import javax.swing.Timer; 6  7 public class Main { 8  9   public static void main(String[] args) {10    ActionListener listener= new Printer();11    Timer timer=new Timer(1000,listener);12     timer.start();13    JOptionPane.showMessageDialog(null, "Quit");14    System.exit(0);15   }16 }

升级版(匿名内部类)

 1 package ares.present; 2  3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.util.Date; 6 import javax.swing.JOptionPane; 7 import javax.swing.Timer; 8  9 public class Main {10 11   public static void main(String[] args) {12     ActionListener listener=new ActionListener() {13       14       @Override15       public void actionPerformed(ActionEvent e) {16        Date date=new Date();17         System.out.println(date);18       }19     };20     21     Timer timer=new Timer(1000,listener);22     timer.start();23     JOptionPane.showMessageDialog(null, "Quit");24     System.exit(0);25   }26 }