「java如何计时器」java做一个可以计时的秒表

博主:adminadmin 2022-11-26 11:17:08 56

今天给各位分享java如何计时器的知识,其中也会对java做一个可以计时的秒表进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何使用JAVA实现实现一个计时器?

public class TestDingShi implements Runnable

{

Thread xc;

Dao dao=new DaoImpl();

public TestDingShi()

{

xc=new Thread(this);

xc.start();

}

public void run()

{

while (true)

{

try

{

xc.sleep(1000);

}

catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

//TODO定时在此

}

}

}

JAVA计时器

/** 每3秒运行一次 */

Timer timer = new Timer();

TimerTask tt = new TimerTask() {

public void run() {

/* 投放炸弹的操作 */

new Thread() {

public void run() {

try {

Thread.sleep(5000);

}catch (Exception e) { }

/* 爆炸的操作 */

}

}.start();

}

}; timer.schedule(tt, 0, 3000);

JAVA计时器,怎么写

import java.awt.BorderLayout;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class TimeCount extends JFrame implements ActionListener{

ThreadCount tc=new ThreadCount(this);

Thread thread=new Thread(tc);

JPanel panelN=new JPanel(),panelC=new JPanel();

JLabel label=new JLabel("计时器");

JButton butnStart=new JButton("开始");

boolean toEnd;

public TimeCount() {

setBounds(100,100,300,300);

setVisible(true);

label.setFont(new Font(null,Font.BOLD,22));

panelN.add(label);

add(panelN,BorderLayout.NORTH);

panelC.add(butnStart);

add(panelC,BorderLayout.CENTER);

butnStart.addActionListener(this);

validate();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent arg0) {

if(arg0.getSource()==butnStart){

if(!thread.isAlive()){

thread=new Thread(tc);

thread.start();

}else {

toEnd=true;

}

}

}

public static void main(String[] args) {

new TimeCount();

}

}

class ThreadCount implements Runnable{

TimeCount lc;

public ThreadCount(TimeCount lc) {

super();

this.lc = lc;

}

public void run() {

int i=1;

while(true){

if(lc.toEnd){

lc.toEnd=false;

lc.butnStart.setText("开始");

return;

}

try {

Thread.sleep(2);

} catch (InterruptedException e) {

// TODO: handle exception

}

i+=2;

int min=i/60000;

int second=(i%60000)/1000;

int mm=i%1000;

String show="";

if(min0)

show+=min+":";

if(second0)

show+=second+".";

show+=mm;

lc.label.setText(show);

}

}

}

满意请采纳。

用java怎么样编写一个针对某个任务的计时器。刚刚学习java,求详解。。。

/**

* 定时器功能演示

* 一秒为单位计时器 可以改变参数

*/

import java.sql.Date;

import java.text.SimpleDateFormat;

import java.util.Timer;

import java.util.TimerTask;

public class Demo12 {

public static void main(String[] args) {

final Timer timer = new Timer();

timer.schedule(new TimerTask(){

public void run(){

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

long now = System.currentTimeMillis();

System.out.println(fmt.format(now));

}

}, 0, 1000);

Date date = new Date(System.currentTimeMillis()+10000);

timer.schedule(new TimerTask(){

@Override

public void run() {

timer.cancel();//取消所有的计划任务

}}, date);

}

}

用JAVA编写计时器

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

public class Calculagraph extends Thread {

public static void main(String[] args) {

new Calculagraph().start();

}

private long now = 0l;

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

private long time;

public void run() {

while (true) {

now = System.currentTimeMillis();// 获取一秒之后的毫秒值

time = now - start;// 两个时间相减的到毫秒差

System.out.format("%02d:%02d:%02d\n",

time / (1000 * 60 * 60) % 60/* 时 */, 

time / (1000 * 60)% 60/* 分 */, 

time / 1000 % 60/* 秒 */);// 格式化字符串输出

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

关于java如何计时器和java做一个可以计时的秒表的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-26,除非注明,否则均为首码项目网原创文章,转载请注明出处。