zoukankan      html  css  js  c++  java
  • 使用Timer类的两个实例 动态时钟

     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中。目前还分不清楚这两种情况应该如何应用?

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

  • 相关阅读:
    编程语言 编译型语言与解释型语言及汇编语言
    Alpha冲刺2
    事后诸葛亮
    Alpha阶段项目复审
    PostgreSQL的表膨胀与Vacuum和Vacuum Full
    PostgreSQL查看表膨胀(对所有表进行膨胀率排序)
    python中from __future__ import division/print_function的用法
    【IT企业笔试集】2013年阿里巴巴实习生招聘笔试题目及解答
    【WPF编程】一个未完成的12306本地客户端的源代码
    【深入理解计算机系统第二版】3.55习题
  • 原文地址:https://www.cnblogs.com/xingzhui/p/5727256.html
Copyright © 2011-2022 走看看