java显示停留代码_求JAVA的秒表源代码,有暂停等功能

展开全部

纯Java做的秒表e5a48de588b662616964757a686964616f31333264636261:

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 

msg += "0" + hours + ":";

}

else{

msg += hours + ":";

}

if(minutes 

msg += "0" + minutes + ":";

}

else{

msg += minutes + ":";

}

if(seconds 

msg += "0" + seconds + ":";

}

else{

msg += seconds + ":";

}

if(ms 

msg += "00" + ms;

}

else if(ms 

msg += "0" + ms;

}

else{

msg += ms;

}

lblTime.setText(msg);

count++;

try {

Thread.sleep(1);

}

catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

运行界面:

87ad10ce95df773ff5d14f1c9531d8e6.bmp

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值