Java程序显示正在运行的时钟(timer类)

 


(本程序主要来自Java语言程序设计(基础篇)(Y.Daniel Liang))

类javax.swing.Timer是一个按照预定频率触发ActionEvent事件的源组件。一个Timer对象可以作为ActionEvent事件的源,监听器必须是ActionListener的实例并且要用Timer对象注册。利用给定的时延和监听器可以使用它唯一的构造方法创建Timer对象其中时延是两个事件行为之间间隔的毫秒数。可以使用addActionListener方法添加其他监听器,用setDelay方法调整时延。用start()方法开始,stop()方法停止。

效果图

首先是ClockAnimation类Java程序显示正在运行的时钟(timer类)

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;



public class ClockAnimation extends StillClock{

    public ClockAnimation(){

        //创建Timer对象

        Timer timer=new Timer(1000,new TimerLisener());

        timer.start();

    }

    private class TimerLisener implements ActionListener{

        //创建相关监听器

        public void actionPerformed(ActionEvent arg0) {

            setCurrentTime();

            repaint();

        }

    }

    

    public static void main(String[] args){

        JFrame frame = new JFrame("ClockAnimation");

        ClockAnimation clock=new ClockAnimation();

        frame.add(clock);

        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(200,200);

        frame.setVisible(true);

    }



}

 StillClock类是(继承Jpanel):

import java.awt.*;

import java.util.*;

import javax.swing.*;



public class StillClock extends JPanel{

    private int hour;

    private int minute;

    private int second;

    

    public StillClock(){

        setCurrentTime();

    }

    

    public StillClock(int hour,int minute,int second){

        this.hour=hour;

        this.minute=minute;

        this.second=second;

    }

    

    public int getHour(){

        return hour;

    }

    

    public void setHour(int hour){

        this.hour=hour;

        repaint();

    }

    

    public int getMinute(){

        return minute;

    }

    public void setMinute(int minute){

        this.minute = minute;

        repaint();

    }

    public int getSecond(){

        return second;

    }

    public void setSecond(){

        this.second=second;

        repaint();

    }

    

    protected void paintComponent(Graphics g){

        

        super.paintComponent(g);

        

        int clockRadius=(int)(Math.min(getWidth(), getHeight())*0.4);//*0.8*0.5);

        int xCenter=getWidth()/2;

        int yCenter=getHeight()/2;

        //System.out.println(clockRadius);

        g.setColor(Color.black);

        g.drawOval(xCenter-clockRadius, yCenter-clockRadius, 2*clockRadius, 2*clockRadius);

        g.drawString("12", xCenter-5, yCenter-clockRadius+12);

        g.drawString("9", xCenter-clockRadius+3, yCenter+5);

        g.drawString("3", xCenter+clockRadius-10, yCenter+3);

        g.drawString("6", xCenter-3, yCenter+clockRadius-3);

        

        //draw second hand

        int sLength=(int)(clockRadius*0.8);

        int xSecond=(int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));

        int ySecond=(int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));

        g.setColor(Color.red);

        g.drawLine(xCenter, yCenter, xSecond, ySecond);

        

        //draw minute hand

        int mLength=(int)(clockRadius*0.65);

        int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)));

        int yMinute=(int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/60)));

        g.setColor(Color.blue);

        g.drawLine(xCenter, yCenter, xMinute, yMinute);

        

        //draw hour hand

        int hLength=(int)(clockRadius*0.5);

        int xHour=(int)(xCenter+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));

        int yHour=(int)(yCenter-hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));

        g.setColor(Color.black);

        g.drawLine(xCenter, yCenter, xHour, yHour);

    }

    

    public void setCurrentTime(){

        Calendar calendar = new GregorianCalendar();

        this.hour=calendar.get(Calendar.HOUR_OF_DAY);

        this.minute=calendar.get(Calendar.MINUTE);

        this.second=calendar.get(Calendar.SECOND);

    }

}

 

你可能感兴趣的:(timer)