「java模拟秒表」java秒表类

博主:adminadmin 2023-01-25 00:00:09 513

本篇文章给大家谈谈java模拟秒表,以及java秒表类对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java编写一个类实现秒表的功能

java System.currentTimeMillis() 就是获取当前的毫秒数

开始时记录 istart= System.currentTimeMillis();

结束时 记录 iend= System.currentTimeMillis();

分钟就是 Math.round((iend-istart)/(60*1000));

那 秒数 就是 Math.round((iend-istart)/1000)%60

再开启一个定时器,定时获取 itmp= System.currentTimeMillis();计算分钟和秒数 显示出来

显示动态在跳得秒和分

java怎样设计一个数字秒表?

思路:

1.声明变量:【开始时间】,【结束时间】,【总时间】。都声明成long类型。

2.建立四个按钮,【开始】【暂停】【继续】【停止】

3.【开始】绑定方法:把系统当前时间赋值给【开始时间】=System.currentTimeMillis();

4.【暂停】绑定方法:把系统当前时间赋值给【结束时间】=System.currentTimeMillis();

然后【结束时间】减去【开始时间】的值赋给【总时间】并显示出来。

5.【继续】绑定方法:把系统当前时间赋值给【开始时间】=System.currentTimeMillis();

6.【停止】绑定方法:把系统当前时间赋值给【结束时间】=System.currentTimeMillis();

然后【结束时间】减去【开始时间】的值赋给【总时间】并显示出来。

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编写一个秒表小程序

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

public class Clock {

public static void main(String[] args) throws Exception{

time();

}

private static void time() throws Exception{

final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

//定时任务

TimerTask task = new TimerTask() {

@Override

public void run() {

System.out.println("当前时间:"+format.format(new Date()));

}

};

//创建定时器

Timer timer = new Timer();

//开始时间

long delay = 0;

//延迟时间

long intevalPeriod = 1 * 1000;

//开始执行定时任务

timer.scheduleAtFixedRate(task, delay, intevalPeriod);

}

}

求java实现秒表程序的简单办法?

Timer clock=new Timer(1000,new ActionListener(){

public void actionPerformed(ActionEvent e){

//每一秒你要做的工作

}

});

//然后要启动秒表,调用这个对象的start()方法,重启restart()方法,停止stop()方法。

关于java模拟秒表和java秒表类的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。