Java写的秒表

TimeRun t=new TimeRun();

t.run();

}

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

class TimeRun extends Thread implements ActionListener{

JLabel jl=new JLabel(“时间”);

JTextField jtf=new JTextField(" ");

JLabel jl2=new JLabel(“秒”);

JButton start=new JButton(“开始”);

JButton stop=new JButton(“暂停”);

JButton clear=new JButton(“清零”);

JPanel jp1=new JPanel();

JPanel jp2=new JPanel();

private float time=0;

private boolean Run=false;

private String time2str(float t) {

int h = (int)t/36000;

int m = ((int)t-h*36000)/600;

double s = (t%600)/10.00;

return String.format(“%02d : %02d : %04.1f”, h,m,s);

}

public TimeRun(){

jl.setAlignmentX(JLabel.CENTER_ALIGNMENT);

jl2.setAlignmentX(JLabel.CENTER_ALIGNMENT);

jtf.setHorizontalAlignment(JTextField.CENTER);

jtf.setColumns(15);

jp1.add(jl,BorderLayout.WEST);

jp1.add(jtf,BorderLayout.CENTER);

jp1.add(jl2,BorderLayout.EAST);

jp2.setLayout(new FlowLayout());

jp2.add(start);

jp2.add(stop);

jp2.add(clear);

StopWatch.jf.add(jp1,BorderLayout.CENTER);

StopWatch.jf.add(jp2,BorderLayout.SOUTH);

StopWatch.jf.setVisible(true);

start.addActionListener(this);

stop.addActionListener(this);

clear.addActionListener(this);

}

public void run() {

jtf.setText(time2str(time));

while (!this.isAlive() && !this.isInterrupted()) {

if (Run) {

jtf.setText(time2str(time));

try {

Thread.sleep(100);

} catch (InterruptedException e1) {

}

time += 1;

}

}

}

public void actionPerformed(ActionEvent e){

String event=e.getActionCommand();

if(event==“开始”){

StopWatch.jf.setTitle(“Start…”);

Run=true;

start.setEnabled(false);

stop.setEnabled(true);

clear.setEnabled(false);

}else if(event==“暂停”){

Run=false;

StopWatch.jf.setTitle(“Pause…”);

stop.setEnabled(false);

start.setEnabled(true);

clear.setEnabled(true);

}else if(event==“清零”){

time=0;

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
学习之门!**

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 18
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Java Swing秒表代码示例: ``` import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Stopwatch extends JFrame implements ActionListener { JLabel timeLabel; JButton startButton, stopButton, resetButton; long startTime = 0; boolean running = false; Timer timer; public Stopwatch() { setTitle("Stopwatch"); setSize(300,150); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(2, 1)); timeLabel = new JLabel("Time: 0.000"); timeLabel.setFont(new Font("Verdana",Font.PLAIN,25)); timeLabel.setHorizontalAlignment(JLabel.CENTER); panel.add(timeLabel); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); startButton = new JButton("Start"); startButton.addActionListener(this); buttonPanel.add(startButton); stopButton = new JButton("Stop"); stopButton.addActionListener(this); buttonPanel.add(stopButton); resetButton = new JButton("Reset"); resetButton.addActionListener(this); buttonPanel.add(resetButton); panel.add(buttonPanel); add(panel); } public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { if (!running) { startTime = System.currentTimeMillis(); timer = new Timer(10, new ActionListener() { public void actionPerformed(ActionEvent e) { long currentTime = System.currentTimeMillis(); double elapsedTime = (currentTime - startTime) / 1000.0; timeLabel.setText(String.format("Time: %.3f", elapsedTime)); } }); timer.start(); running = true; } } else if (e.getSource() == stopButton) { if (running) { timer.stop(); running = false; } } else if (e.getSource() == resetButton) { running = false; timeLabel.setText("Time: 0.000"); } } public static void main(String[] args) { Stopwatch stopwatch = new Stopwatch(); stopwatch.setVisible(true); } } ``` 这个示例程序创建了一个带有“开始”、“停止”和“重置”按钮的窗口,按下“开始”按钮时会启动一个计时器,按下“停止”按钮时会停止计时器,按下“重置”按钮时会将计时器归零并停止。计时器以毫秒为单位计算时间,并将结果显示在标签中。 希望这可以帮助您开始编您自己的Java Swing秒表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值