Java 简易计时器

Java简易计时器

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.*;
import javax.swing.Timer;
import java.text.*;
public class time extends JFrame implements ActionListener{
	Timer time;
	JTextField text;
	JButton bStart,bStop,bGo;
	//SimpleDateFormat m;
	long t = 0;
	time(){
		time= new Timer(1000,this);              //timer(int a,object b)  time 对象做计时器的监视器;
		//m=new SimpleDateFormat("hh:mm:ss");         //设置时钟样式;
		text = new JTextField(10);
		bStart = new JButton("start");
		bStop = new JButton("stop");
		bGo =new JButton("go on");
		bStart.addActionListener(this);
		bStop.addActionListener(this);
		bGo.addActionListener(this);
		setLayout(new FlowLayout());         
		add(bStart);
		add(bStop);
		add(bGo);
		add(text);
		setSize(500,500);
		validate();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==time)
		{
			t=t+1;
			int s=(int)t%60;
			int m=(int)t/60%60;
			int h=(int)t/3600;
			text.setText("时间:  "+h+":"+m+":"+s);
			
		}
		if(e.getSource()==bStart)
		{
			time.start();
		}
		if(e.getSource()==bStop)
		{
			time.stop();
		}
		if(e.getSource()==bGo)
		{
			time.restart();
		}
	}
	
}

运行截图
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值