java做简易秒表_java秒表小程序编写

这篇博客分享了一个使用Java编写的简易秒表小程序,通过JApplet、ActionListener等组件实现。程序中包含开始和结束按钮,能够实时更新并显示时间。
摘要由CSDN通过智能技术生成

8b6a0d1fd8dd3fdab5a2096fcd237d6b.png

一别经年

回答时间:2019-12-05

向TA提问

收藏的一个小程序。

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 JApplet implements ActionListener{

private JLabel lblTime=new JLabel("时间");

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

private JButton btnEnd=new JButton("结束");

private long startTime=System.currentTimeMillis();

private boolean isRun=false;

private NumberFormat numberFormat=new DecimalFormat("0.000");

public TimerApplet(){

this.setLayout(new FlowLayout());

this.add(lblTime);

this.add(btnStart);

this.add(btnEnd);

btnStart.addActionListener(this);

btnEnd.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==btnStart){

class TimerThread implements Runnable{

public void run() {

isRun=true;

startTime=System.currentTimeMillis();

while(isRun){

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

double time=(System.currentTimeMillis()-startTime);

lblTime.setText(numberFormat.format(time/1000));

}

}

}

new Thread(new TimerThread()).start();

}

if(e.getSource()==btnEnd){

isRun=false;

}

}

}

收起

6f93e2c92fc5e58e2ec18df1db0b9f08.png

阅读全文

9ded402a52cdd2b9481e864a5093252c.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值