码迷,mamicode.com
首页 > 编程语言 > 详细

java时钟

时间:2015-12-16 00:05:16      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

 一般实现形式

 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     @Override
10     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             @Override
15             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 }

java时钟

标签:

原文地址:http://www.cnblogs.com/liunlls/p/timer.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!