java秒表代码_求一个java秒表代码

展开全部

package info.bioz.test;

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.*;

import java.util.Date;

import java.text.SimpleDateFormat;

/**

*

File: StopWatch.java

*

Description:

*

BIOZ.info Copyright (c) 2004

*

* @author Chance

*/

public class StopWatch extends JFrame {

JButton btnStart,btnStop;

JLabel label;

Timer timer;

public StopWatch() {

label=new JLabel("00:00:00.000");

btnStart=new JButton("start");

btnStop=new JButton("stop");

final int delay=100;

final Date startTime=new Date();

final SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss.S");

final Action taskPerformer = new AbstractAction() {

public void actionPerformed(ActionEvent evt) {

//显示时间

Date d=new Date(System.currentTimeMillis()-startTime.getTime()-28800000);

label.setText(sdf.format(d));

label.repaint();

}

};

btnStart.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt) {

startTime.setTime(new Date().getTime());

timer=new Timer(delay, taskPerformer);

timer.start();

}

});

btnStop.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt) {

if(timer!=null&&timer.isRunning())

timer.stop();

}

});

Container c=getContentPane();

c.add(label,BorderLayout.NORTH);

c.add(btnStart,BorderLayout.CENTER);

c.add(btnStop,BorderLayout.SOUTH);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

private static void createAndShowGUI() {

StopWatch window=new StopWatch();

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.pack();

window.setVisible(true);

}

}

希望我的回答能够32313133353236313431303231363533e78988e69d8331333233663431帮到你

祝好运!

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值