怎么用JAVA编一个秒表?要用到什么函数?!

如题所述

纯Java做的秒表:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestTimer extends JFrame implements ActionListener, Runnable {
private static TestTimer obj;
private JButton btnStart;
private JButton btnPause;
private JButton btnResume;
private JButton btnStop;
private JLabel lblTime;
private static Thread th;
private long count;

public TestTimer(){
super("秒表");
btnStart = new JButton("开始");
btnPause = new JButton("暂停");
btnResume = new JButton("继续");
btnStop = new JButton("停止");
lblTime = new JLabel("00:00:00.000");
this.setLayout(new FlowLayout());
this.add(btnStart);
this.add(btnPause);
this.add(btnResume);
this.add(btnStop);
this.add(lblTime);
btnStart.addActionListener(this);
btnPause.addActionListener(this);
btnResume.addActionListener(this);
btnStop.addActionListener(this);
this.setSize(150, 200);
this.setVisible(true);
}

public static void main(String[] args) {
obj = new TestTimer();
}

public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn.getText().equals("开始")){
th = new Thread(obj);
count = 0;
th.start();
}
else if(btn.getText().equals("暂停")){
th.suspend();
}
else if(btn.getText().equals("继续")){
th.resume();
}
else if(btn.getText().equals("停止")){
th.stop();
}
}

@Override
public void run() {
while(true){
int ms, seconds, minutes, hours;
String msg = "";
hours = (int)(count / 3600000);
minutes = (int)((count - hours * 3600000) / 60000);
seconds = (int)((count - hours * 3600000 - minutes * 60000) / 1000);
ms = (int)(count % 1000);
if(hours < 10){
msg += "0" + hours + ":";
}
else{
msg += hours + ":";
}
if(minutes < 10){
msg += "0" + minutes + ":";
}
else{
msg += minutes + ":";
}
if(seconds < 10){
msg += "0" + seconds + ":";
}
else{
msg += seconds + ":";
}
if(ms < 10){
msg += "00" + ms;
}
else if(ms < 100){
msg += "0" + ms;
}
else{
msg += ms;
}

lblTime.setText(msg);
count++;
try {
Thread.sleep(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答

怎么用JAVA编一个秒表?要用到什么函数?!
纯Java做的秒表:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TestTimer extends JFrame implements ActionListener, Runnable { private static TestTimer obj;private JButton btnStart;private JButton btnPause;private JButton btnResume;private JButton btnStop;p...

求一个倒计时,秒表。需要JAVA. 需要有断电记忆功能
import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class TimerExample extends JFrame implements ActionListener { private static final long serialVersionUID = 1L;private JLabel countDownLabel, stopWatchLabel;private JButton countDownButton, stopWatch...

java 这是一个秒表,我想把显示的时间变成系统时间要怎么做,具体操作
简单的写了一个时间显示的程序 时间显示的格式 时:分:秒 毫秒 参考代码如下 import java.awt.*;import java.awt.event.*;import java.text.SimpleDateFormat;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.Timer;\/\/注意导入的是javax.swing.Timer; 有一些类似的包不要...

java编程:秒表程序,可实现计算秒数,到达一小时的话,程序终止。_百度知 ...
我用android写了一个计时器, java写的话更简单的, 直接用Thread线程暂停即可, 用java控制台程序吗.

java秒表小程序编写
收藏的一个小程序。import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.DecimalFormat;import java.text.NumberFormat;import javax.swing.JApplet;import javax.swing.JButton;import javax.swing.JLabel;public class TimerApplet extends J...

求一个java秒表代码
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...

Java中的秒表-StopWatch
在评估业务代码或算法性能时,时间复杂度和实际运行时间都是关键指标。本文将探讨Java中的StopWatch工具类,其用途在于直观地衡量程序的执行速度。StopWatch并非Java标准库的一部分,通常在Apache Commons Lang或Spring Framework的库中找到。Apache Commons Lang中的StopWatch功能类似于日常使用的秒表,提供了多...

求解释一个JAVA(秒表)的小程序,可以给代码标记注释的。新手,看不懂代码...
import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.*;\/\/---以上部分为导入需要的文件public class TimeViewer extends Applet implements ActionListener,Runnable{ \/\/所需要的数据定义Thread timer;\/\/定义一个线程,用于每一秒去更新一次时间文字TextField in,out;\/\/...

JAVA秒表倒计时程序,用swing界面显示,请大家一定要帮帮小弟哈,我实在做...
import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Tidy extends JFrame{private static final long serialVersionUID = 1L;private static final String[] NS = { "秒表倒计时器", "时", "分", "秒", "请输入...

写一个计时器 JAVA代码是什么?
} public void timeVoid(){ final Timer timer = new Timer();TimerTask tt=new TimerTask() { Override public void run() { System.out.println("到点啦!");timer.cancel();} };timer.schedule(tt, 3000);} } 整合的:\/ java倒计时器 shiyang \/ package com.sy.game.test;import ...

相似回答