java程序设计 秒表计时器_【Java】Java计时器(秒表)

这个Java程序设计了一个秒表计时器,利用CountingThread类进行后台计时,并通过JFrame展示时间。用户可以点击"开始"按钮启动计时,点击"暂停"按钮暂停计时,以及点击"清零"按钮重置计时器。计时器以小时:分钟:秒 毫秒的格式显示时间。
摘要由CSDN通过智能技术生成

import javax.swing.*;importjava.awt.HeadlessException;importjava.awt.BorderLayout;importjava.awt.FlowLayout;importjava.awt.Font;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;/*** 计时器*/

public class Timer extendsJFrame {/****/

private static final long serialVersionUID = 1L;private static final String INITIAL_LABEL_TEXT = "00:00:00 000";//计数线程

private CountingThread thread = newCountingThread();//记录程序开始时间

private long programStart =System.currentTimeMillis();//程序一开始就是暂停的

private long pauseStart =programStart;//程序暂停的总时间

private long pauseCount = 0;private JLabel label = newJLabel(INITIAL_LABEL_TEXT);private JButton startPauseButton = new JButton("开始");private JButton resetButton = new JButton("清零");private ActionListener startPauseButtonListener = newActionListener() {public voidactionPerformed(ActionEvent e) {if(thread.stopped) {

pauseCount+= (System.currentTimeMillis() -pauseStart);

thread.stopped= false;

startPauseButton.setText("暂停");

}else{

pauseStart=System.currentTimeMillis();

thread.stopped= true;

startPauseButton.setText("继续");

}

}

};private ActionListener resetButtonListener = newActionListener() {public voidactionPerformed(ActionEvent e) {

pauseStart=programStart;

pauseCount= 0;

thread.stopped= true;

label.setText(INITIAL_LABEL_TEXT);

startPauseButton.setText("开始");

}

};public Timer(String title) throwsHeadlessException {super(title);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocation(300, 300);

setResizable(false);

setupBorder();

setupLabel();

setupButtonsPanel();

startPauseButton.addActionListener(startPauseButtonListener);

resetButton.addActionListener(resetButtonListener);

thread.start();//计数线程一直就运行着

}//为窗体面板添加边框

private voidsetupBorder() {

JPanel contentPane= new JPanel(newBorderLayout());

contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));this.setContentPane(contentPane);

}//配置按钮

private voidsetupButtonsPanel() {

JPanel panel= new JPanel(newFlowLayout());

panel.add(startPauseButton);

panel.add(resetButton);

add(panel, BorderLayout.SOUTH);

}//配置标签

private voidsetupLabel() {

label.setHorizontalAlignment(SwingConstants.CENTER);

label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));this.add(label, BorderLayout.CENTER);

}//程序入口

public static voidmain(String[] args) {try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exception e) {

e.printStackTrace();

}

Timer frame= new Timer("计时器");

frame.pack();

frame.setVisible(true);

}private class CountingThread extendsThread {public boolean stopped = true;privateCountingThread() {

setDaemon(true);

}

@Overridepublic voidrun() {while (true) {if (!stopped) {long elapsed = System.currentTimeMillis() - programStart -pauseCount;

label.setText(format(elapsed));

}try{

sleep(1); //1毫秒更新一次显示

} catch(InterruptedException e) {

e.printStackTrace();

System.exit(1);

}

}

}//将毫秒数格式化

private String format(longelapsed) {inthour, minute, second, milli;

milli= (int) (elapsed % 1000);

elapsed= elapsed / 1000;

second= (int) (elapsed % 60);

elapsed= elapsed / 60;

minute= (int) (elapsed % 60);

elapsed= elapsed / 60;

hour= (int) (elapsed % 60);return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值