java时钟和计时器

java时钟和计时器_第1张图片

先来张作品效果图,用java实现的一个小工具。十分的简单,就是对 Timer类的一个应用。
//显示主面板
ClockFrame.java

package MyClock;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;


public class ClockFrame extends JFrame implements ActionListener {

	private JButton but_start = null;
	private JButton but_stop = null;
	private JButton but_reset = null;
	private JLabel jClock = null;
	private JLabel jTiming = null;
	private DoClock dClock = null;
	private Font font1 = null;
	private Timer tmr = null;
	
//	private boolean flag = false; //表示是否正在走
	private String timeString = null; //设置初始时间
	
	public ClockFrame() {
		this.InitComponent();
		setLayout(null); //绝对定位
		setResizable(false); //窗口不能变大小
		/**
		 * 设置没1秒做一个动作
		 */
		this.tmr = new Timer(1000, this);
		add(but_start);
		add(but_stop);
		add(but_reset);
		add(jClock);
		add(jTiming);
		
		MyEvent();
	}
	
	public void InitWindow() {
		this.setTitle("计时器");
		this.setSize(400, 300); //宽,高
		this.setLocationRelativeTo(null); //居中显示
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setVisible(true);
		
		dClock = new DoClock(jClock);
		new Thread(dClock).start();
	}
	
	public void InitComponent() {
		font1 = new Font("楷体", Font.BOLD, 20);
		
		but_start = new JButton("开始");
		but_start.setBounds(50, 190, 80, 40);
		but_start.addActionListener(this);
		
		but_stop = new JButton("停止");
		but_stop.setBounds(160, 190, 80, 40);
		
		but_reset = new JButton("复原");
		but_reset.setBounds(270, 190, 80, 40);
		
		jClock = new JLabel("");
		jClock.setBounds(160, 100, 100, 30);
		
		timeString = "00:10";
		jTiming = new JLabel(timeString);
		jTiming.setBounds(158, 66, 88, 30);
		jTiming.setFont(font1);
	}
	
	public void MyEvent() {
		//关闭窗口
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		but_stop.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				tmr.stop();
			}
			
		});
		
		but_reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				tmr.stop();
				jTiming.setText(timeString);
			}
			
		});
	}
	
	/**
	 * 添加一个事件监听,来显示计时时间的变化
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == but_start) {
			if(jTiming.getText().equals("00:00")) {
				jTiming.setText(timeString);
			}
			tmr.start();
//			flag = true;
        } else {
         
            jTiming.setText(getMyTime.getDoTiming(jTiming.getText()));
            
            if(jTiming.getText().equals("00:00"))
            	 tmr.stop();
        }
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClockFrame cf = new ClockFrame();
		cf.InitWindow();
	}


}
//获取时间转成格式化字符串
getMyTime.java

package MyClock;

import java.util.Date;

public class getMyTime {

	public static String getTime() {
		Date now=new Date();
		String ansString = getZero(now.getHours()) + ":" + getZero(now.getMinutes()) + ":"
				+ getZero(now.getSeconds());
		return ansString;
	}
	
	public static String getZero(int a) {
		if(a < 10) 
			return "0"+a;
		return ""+a;
	}
	
	public static String getDoTiming (String dotime) {
		
		int m = Integer.parseInt(dotime.substring(0, 2));
		int s = Integer.parseInt(dotime.substring(3, 5));
//		System.out.println(m + "@@" + s);
		
		if(s == 0) {
			s=59;
			m -= 1;
		}else {
			s -= 1;
		}
		String mString = getZero(m);
		String sString = getZero(s);
		
		return mString + ":" + sString;
	}
	
	/*public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(getDoTiming("00:01"));
		System.out.println(getDoTiming("04:00"));
	}*/

}

//开线程显示时钟时间
DoClock.java

package MyClock;


import javax.swing.JLabel;

public class DoClock implements Runnable {

	private JLabel jLabel = null;
	
	public DoClock(JLabel jLabel) {
		this.jLabel = jLabel;
	}
	

	@Override      //时钟  来一个线程  来显示时钟时间
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			jLabel.setText(getMyTime.getTime());
		}
	}
	
	/*public static void main(String[] args) {
		// TODO Auto-generated method stub

	}*/


}


 
   

你可能感兴趣的:(个人小作品)