Java 向秒表添加恢复功能
我编写了一个简单的秒表,它有三个功能。首先,我有一个开始按钮来开始秒表,一个暂停按钮来暂停秒表,最后一个重置按钮来重置整个秒表 当我按下暂停按钮时,秒表会暂停,比如说10秒。当我恢复秒表时(再次按下开始按钮),秒表不会从10.0秒开始恢复。它从我暂停的时间量和当前时间恢复。例如,如果我暂停5秒并点击resume,秒表将从15.0秒开始计时 我知道Java 向秒表添加恢复功能,java,swing,Java,Swing,我编写了一个简单的秒表,它有三个功能。首先,我有一个开始按钮来开始秒表,一个暂停按钮来暂停秒表,最后一个重置按钮来重置整个秒表 当我按下暂停按钮时,秒表会暂停,比如说10秒。当我恢复秒表时(再次按下开始按钮),秒表不会从10.0秒开始恢复。它从我暂停的时间量和当前时间恢复。例如,如果我暂停5秒并点击resume,秒表将从15.0秒开始计时 我知道Swing.Timer中没有实际的暂停功能。有没有办法解决这个问题,让秒表恢复正常 如有任何建议,将不胜感激 代码: 从概念上讲,这个想法是,你想要跟踪
Swing.Timer
中没有实际的暂停功能。有没有办法解决这个问题,让秒表恢复正常
如有任何建议,将不胜感激
代码:
从概念上讲,这个想法是,你想要跟踪秒表的“总运行”,这就是它一直处于活动状态的所有总持续时间 有很多方法可以实现这一点,其中之一可能是简单地保持一个运行总数,只有在秒表停止或暂停时才会更新。秒表的“持续时间”是“当前”循环的“当前持续时间”和“之前的总持续时间”的总和 类似于
public class StopWatch {
private LocalDateTime startTime;
private Duration totalRunTime = Duration.ZERO;
public void start() {
startTime = LocalDateTime.now();
}
public void stop() {
Duration runTime = Duration.between(startTime, LocalDateTime.now());
totalRunTime = totalRunTime.plus(runTime);
startTime = null;
}
public void pause() {
stop();
}
public void resume() {
start();
}
public void reset() {
stop();
totalRunTime = Duration.ZERO;
}
public boolean isRunning() {
return startTime != null;
}
public Duration getDuration() {
Duration currentDuration = Duration.ZERO;
currentDuration = currentDuration.plus(totalRunTime);
if (isRunning()) {
Duration runTime = Duration.between(startTime, LocalDateTime.now());
currentDuration = currentDuration.plus(runTime);
}
return currentDuration;
}
}
好的,开始
和停止
基本上与暂停
和恢复
相同,但你明白了
还有一个可运行的例子
现在,本例持续运行Swing计时器,但秒表可以暂停
和恢复
,关键是要证明秒表实际上工作正常;)
从概念上讲,这个想法是,你想要跟踪秒表的“总运行”,这就是它一直处于活动状态的所有总持续时间
有很多方法可以实现这一点,其中之一可能是简单地保持一个运行总数,只有在秒表停止或暂停时才会更新。秒表的“持续时间”是“当前”循环的“当前持续时间”和“之前的总持续时间”的总和
类似于
public class StopWatch {
private LocalDateTime startTime;
private Duration totalRunTime = Duration.ZERO;
public void start() {
startTime = LocalDateTime.now();
}
public void stop() {
Duration runTime = Duration.between(startTime, LocalDateTime.now());
totalRunTime = totalRunTime.plus(runTime);
startTime = null;
}
public void pause() {
stop();
}
public void resume() {
start();
}
public void reset() {
stop();
totalRunTime = Duration.ZERO;
}
public boolean isRunning() {
return startTime != null;
}
public Duration getDuration() {
Duration currentDuration = Duration.ZERO;
currentDuration = currentDuration.plus(totalRunTime);
if (isRunning()) {
Duration runTime = Duration.between(startTime, LocalDateTime.now());
currentDuration = currentDuration.plus(runTime);
}
return currentDuration;
}
}
好的,开始
和停止
基本上与暂停
和恢复
相同,但你明白了
还有一个可运行的例子
现在,本例持续运行Swing计时器,但秒表可以暂停
和恢复
,关键是要证明秒表实际上工作正常;)
所以,“基本”概念是——你需要跟踪秒表的总破坏时间,也就是从它第一次启动到暂停的时间。当继续时,您会将“新运行”时间添加到以前的tallypanel.setLayout(null)中;所以,“基本”概念是——你需要跟踪秒表的总破坏时间,也就是从它第一次启动到暂停的时间。当继续时,您会将“新运行”时间添加到以前的tallypanel.setLayout(null)中;因此,Instant.now()
与使用LocalDateTime.now()
?@LordJesus老实说,我只知道如何处理LocalDateTime
:P@LordJesus做了一个快速检查,将LocalDateTime
替换为Instant
,看起来效果不错。您的解决方案很好,但它编写的代码比我的多得多。我想,为了跟踪“运行”时间,这可能是最好的方法?我还将比较您的解决方案和我的解决方案。我想区别在于你添加了更多的逻辑代码来记录时间,然后单击resume,你将其添加回之前的计数。我说得对吗?所以,在我的情况下,Instant.now()
与使用LocalDateTime.now()
?@老实说,我只知道如何处理LocalDateTime
:P@LordJesus做了一个快速检查,将LocalDateTime
替换为Instant
,看起来效果不错。您的解决方案很好,但它编写的代码比我的多得多。我想,为了跟踪“运行”时间,这可能是最好的方法?我还将比较您的解决方案和我的解决方案。我想区别在于你添加了更多的逻辑代码来记录时间,然后单击resume,你将其添加回之前的计数。我说得对吗?
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalDateTime;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) throws InterruptedException {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
private JButton btn;
private StopWatch stopWatch = new StopWatch();
private Timer timer;
public TestPane() {
label = new JLabel("...");
btn = new JButton("Start");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(label, gbc);
add(btn, gbc);
timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText(Long.toString(stopWatch.getDuration().getSeconds()));
}
});
timer.start();
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (stopWatch.isRunning()) {
stopWatch.pause();
btn.setText("Start");
} else {
stopWatch.resume();
btn.setText("Pause");
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
public class StopWatch {
private LocalDateTime startTime;
private Duration totalRunTime = Duration.ZERO;
public void start() {
startTime = LocalDateTime.now();
}
public void stop() {
Duration runTime = Duration.between(startTime, LocalDateTime.now());
totalRunTime = totalRunTime.plus(runTime);
startTime = null;
}
public void pause() {
stop();
}
public void resume() {
start();
}
public void reset() {
stop();
totalRunTime = Duration.ZERO;
}
public boolean isRunning() {
return startTime != null;
}
public Duration getDuration() {
Duration currentDuration = Duration.ZERO;
currentDuration = currentDuration.plus(totalRunTime);
if (isRunning()) {
Duration runTime = Duration.between(startTime, LocalDateTime.now());
currentDuration = currentDuration.plus(runTime);
}
return currentDuration;
}
}
}