一、秒錶的作用及介紹
秒錶是一種時間計算工具,主要用於測量一段時間,是生活和工作中常見的工具之一。要實現高精度,就需要使用計算機來完成,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/zh-hant/n/303001.html