一、秒表的作用及介绍
秒表是一种时间计算工具,主要用于测量一段时间,是生活和工作中常见的工具之一。要实现高精度,就需要使用计算机来完成,Java编程提供了丰富的API,可以很方便地实现秒表的功能。
一个高精度秒表功能的实现一般包含以下几个关键点:
1、开始计时;
2、暂停计时;
3、继续计时;
4、停止计时;
5、重置计时。
二、Java实现高精度秒表的方法
实现高精度秒表可以使用Java的多线程技术和System.currentTimeMillis()方法。这个方法返回当前时间(以毫秒为单位)。
public class StopWatch {
private long startTime;
private boolean running;
public void start() {
startTime = System.currentTimeMillis();
running = true;
}
public void pause() {
running = false;
}
public void resume() {
running = true;
startTime = System.currentTimeMillis() - (startTime - System.currentTimeMillis());
}
public void stop() {
running = false;
startTime = 0;
}
public long getTime() {
return running ? System.currentTimeMillis() - startTime : 0;
}
}
上面的代码使用了一个StopWatch类,并包含了每个关键点所需的操作。可以通过start()方法开始计时,pause()方法暂停计时,resume()方法继续计时,stop()方法停止计时, getTime()方法获取经过的时间。
三、实现高精度秒表的GUI
为了让秒表更加友好,可以通过GUI来展示秒表的功能。这里使用Java Swing来实现GUI。
首先要创建一个名称为MyStopwatch的类,该类继承自JFrame,然后将StopWatch类和GUI代码集成到一起。下面是完整代码的示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyStopwatch extends JFrame {
private StopWatch stopwatch;
private JLabel timeLabel;
private JButton startButton;
private JButton pauseButton;
private JButton stopButton;
private JButton resumeButton;
public MyStopwatch() {
stopwatch = new StopWatch();
setPreferredSize(new Dimension(300, 100));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
timeLabel = new JLabel("0:00:000");
timeLabel.setFont(new Font("Verdana", Font.PLAIN, 30));
timeLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
add(timeLabel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
startButton = new JButton("Start");
pauseButton = new JButton("Pause");
stopButton = new JButton("Stop");
resumeButton = new JButton("Resume");
buttonPanel.add(startButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
buttonPanel.add(resumeButton);
add(buttonPanel, BorderLayout.SOUTH);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
stopwatch.start();
}
});
pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
stopwatch.pause();
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
stopwatch.stop();
timeLabel.setText("0:00:000");
}
});
resumeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
stopwatch.resume();
}
});
Timer updateTimer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
updateTimeDisplay();
}
});
updateTimer.start();
pack();
setVisible(true);
}
private void updateTimeDisplay() {
int time = (int) (stopwatch.getTime() / 10);
String formattedTime = String.format("%d:%02d:%03d", time / (60 * 100), (time / 100) % 60, time % 100);
timeLabel.setText(formattedTime);
}
public static void main(String[] args) {
new MyStopwatch();
}
}
四、结论
通过上面的代码示例可以看出,在Java中实现高精度的秒表功能非常容易。同时,使用Swing实现GUI也是非常简单的,可以让应用程序更加友好,提高用户体验。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/303001.html