当前位置:首页 » 编程语言 » java计时器

java计时器

发布时间: 2022-12-08 20:22:31

java 秒表

package demo;

import javax.swing.*;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Timer extends JFrame {

private static final long serialVersionUID = 1L;

private static final String INITIAL_LABEL_TEXT = "00:00:00 000";

// 计数线程
private CountingThread thread = new CountingThread();

// 记录程序开始时间
private long programStart = System.currentTimeMillis();

// 程序一开始就是暂停的
private long pauseStart = programStart;

// 程序暂停的总时间
private long pauseCount = 0;

private JLabel label = new JLabel(INITIAL_LABEL_TEXT);

private JButton startPauseButton = new JButton("开始");

private JButton resetButton = new JButton("清零");

private ActionListener startPauseButtonListener = new ActionListener() {
public void actionPerformed(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 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseStart = programStart;
pauseCount = 0;
thread.stopped = true;
label.setText(INITIAL_LABEL_TEXT);
startPauseButton.setText("开始");
}
};

public Timer(String title) throws HeadlessException {
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(300, 300);
setResizable(false);

setupBorder();
setupLabel();
setupButtonsPanel();

startPauseButton.addActionListener(startPauseButtonListener);
resetButton.addActionListener(resetButtonListener);

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

// 为窗体面板添加边框
private void setupBorder() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.setContentPane(contentPane);
}

// 配置按钮
private void setupButtonsPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(startPauseButton);
panel.add(resetButton);
add(panel, BorderLayout.SOUTH);
}

// 配置标签
private void setupLabel() {
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));
this.add(label, BorderLayout.CENTER);
}

// 程序入口
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}

Timer frame = new Timer("计时器");
frame.pack();
frame.setVisible(true);
}

private class CountingThread extends Thread {

public boolean stopped = true;

private CountingThread() {
setDaemon(true);
}

@Override
public void run() {
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(long elapsed) {
int hour, 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);
}
}
}

你可以试试,希望能帮到你!

㈡ 用JAVA编写计时器

计时器可以使用timer类也可以使用线程类来操作,下面是Thread做的简单的计时器

{

publicstaticvoidmain(String[]args){
newCalculagraph().start();
}

privatelongnow=0l;

privatelongstart=System.currentTimeMillis();//程序启动时间的毫秒值

privatelongtime;

publicvoidrun(){
while(true){
now=System.currentTimeMillis();//获取一秒之后的毫秒值
time=now-start;//两个时间相减的到毫秒差
System.out.format("%02d:%02d:%02d ",
time/(1000*60*60)%60/*时*/,
time/(1000*60)%60/*分*/,
time/1000%60/*秒*/);//格式化字符串输出
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}

㈢ java 用计时器写一个程序,每隔一秒打印出当前时间。(不可使用循环语句,使用timer类)

new java.util.Timer().schele(new java.util.TimerTask(){
public void run(){
System.out.println(new java.util.Date());
}
}, 1000, 100);

㈣ java 中timer类的用法是什么

现在项目中用到需要定时去检查文件是否更新的功能。timer正好用于此处。
用法很简单,new一个timer,然后写一个timertask的子类即可。
package comz.autoupdatefile;

import java.util.Timer;
import java.util.TimerTask;

public class M {
public static void main(String[] args) {
// TODO todo.generated by zoer
Timer timer = new Timer();
timer.schele(new MyTask(), 1000, 2000);
}
}

class MyTask extends TimerTask {

@Override
public void run() {
System.out.println("dddd");

}

}
这样,就可以在1秒钟之后开始执行mytask,每两秒钟执行一次。
当然,timer的功能也可以通过自己构造线程,然后在线程中用sleep来模拟停止一段时间,然后再执行某个动作。
其实,看一下timertask的源码就立即可以知道,timertask就是实现了runnable接口的。也就是说,通过timer来间隔一段时间执行一个操作,也是通过一个线程来做到的。

热点内容
建立云存储 发布:2024-05-03 21:04:03 浏览:74
socket编程php 发布:2024-05-03 20:12:50 浏览:207
坦洲邮政局可以解压吗 发布:2024-05-03 20:09:55 浏览:732
二级程序编译答案 发布:2024-05-03 18:41:35 浏览:654
领动自动精英版是哪个配置 发布:2024-05-03 18:37:30 浏览:151
java编译器中cd什么意思 发布:2024-05-03 18:36:00 浏览:390
传奇服务器如何刷钱 发布:2024-05-03 18:36:00 浏览:978
安卓版twitter怎么注册 发布:2024-05-03 18:28:05 浏览:894
Python逻辑优先级 发布:2024-05-03 18:26:14 浏览:268
linux查看svn密码 发布:2024-05-03 18:12:47 浏览:805