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

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

  • 相关阅读:
    1003 Emergency (25分)
    1013 Battle Over Cities (25分)
    1009 Product of Polynomials (25分)
    一元多项式的乘积与和(C++)
    1015 Reversible Primes (20分)
    list()函数对原来类型已经是列表的不会在添加[]
    全局变量把值固定
    python中None与Null的区别
    基础算法之排序
    列表和字符串的方法返回一个新的对象;直接加入到原对象中
  • 原文地址:https://www.cnblogs.com/xingzhui/p/5727256.html
Copyright © 2011-2022 走看看