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

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

  • 相关阅读:
    (转载)python调用shell命令之os 、commands、subprocess
    Nginx的upstream反向代理、负载均衡详解
    安装Nginx到linux服务器(Ubuntu)详解
    Head First Python-python面向对象
    Head First Python-Python简单处理文件
    ctfshow 红包题 武穆遗书
    ciscn_2019_s_3 一道收获很多的题(进步大只能说明基础差)
    攻防世界 pwn welpwn
    get_started_3dsctf_2016
    &pwn1_sctf_2016 &ciscn_2019_n_1 &ciscn_2019_c_1 &ciscn_2019_en_2&
  • 原文地址:https://www.cnblogs.com/xingzhui/p/5727256.html
Copyright © 2011-2022 走看看