首页 > 其他 > 详细

使用Timer类的两个实例 动态时钟

时间:2016-08-01 22:37:20      阅读:705      评论:0      收藏:0      [点我收藏+]
 1 package chapter16;
 2 
 3 import javax.swing.*;
 4 import chapter15.StillClock;
 5 import java.awt.event.*;
 6 
 7 public class ClockAnimation extends JFrame {
 8     public class TimerListener implements ActionListener {
 9         @Override
10         public void actionPerformed(ActionEvent e) {
11             // TODO Auto-generated method stub
12             clock.setCurrentTime();
13             clock.repaint();
14         }
15     }
16     private StillClock clock=new StillClock();
17     public ClockAnimation(){
18         add(clock);
19         Timer timer=new Timer(1000,new TimerListener());
20         timer.start();
21     }
22     public static void main(String[] args) {
23         // TODO Auto-generated method stub
24         ClockAnimation frame=new ClockAnimation();
25         frame.setTitle("ClockAnimation");
26         frame.setSize(200,200);
27         frame.setLocationRelativeTo(null);
28         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29         frame.setVisible(true);
30 
31     }
32 
33 }

第一个实例是动态时钟,其中StillClock是静态时钟的类。

 1 package chapter16;
 2 import javax.swing.*;
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 
 6 public class AnimationDemo extends JFrame {
 7     public AnimationDemo(){
 8         add(new MovingMessagePanel("message moving?"));
 9     }
10     public static class MovingMessagePanel extends JPanel {
11         
12         public class TimerListener implements ActionListener {
13             @Override
14             public void actionPerformed(ActionEvent e) {
15                 // TODO Auto-generated method stub
16                 repaint();
17             }
18         }
19         private String message="Welcome to Java";
20         private int xCoordinate=0;
21         private int yCoordinate=30;
22         
23         public MovingMessagePanel(String message){
24             this.message=message;
25             Timer timer=new Timer(500,new TimerListener());
26             timer.start();
27         }
28         protected void paintComponent(Graphics g){
29             super.paintComponent(g);
30             if(xCoordinate>getWidth()){
31                 xCoordinate=-20;
32             }
33             xCoordinate+=5;
34             g.drawString(message, xCoordinate, yCoordinate);
35         }    
36     }
37     public static void main(String[] args) {
38         // TODO Auto-generated method stub
39         AnimationDemo frame=new AnimationDemo();
40         frame.setTitle("AnimationDemo");
41         frame.setSize(300,100);
42         frame.setLocationRelativeTo(null);
43         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
44         frame.setVisible(true);
45 
46     }
47 
48 }

第二个实例是一个可以横向移动的字符串。

 

两个实例都是采用了Timer类。

不同的地方在于,一个TimerListener定义在JFrame也就是外层框架中,一个TimerListener定义在内层,JPanel中。目前还分不清楚这两种情况应该如何应用?

 

等后面只是丰富来再来解决。

使用Timer类的两个实例 动态时钟

原文:http://www.cnblogs.com/xingzhui/p/5727256.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!