【发布时间】:2018-01-01 00:36:21
【问题描述】:
这里是githublink
所以我试图创建一个应用程序并将其分解为 3 个部分,其中之一是创建一个计时器。这个计时器有两个字段:一个输入分钟,一个输入秒。它应该在几分钟或几秒钟内由用户输入并在应用程序屏幕上显示倒计时。当计时器达到 0 时,它会提醒用户。我到处搜索,在 Java 中找不到这样做的倒数计时器。我发现的只是在控制台中工作的倒数计时器或已经具有由开发人员而不是用户设置的预定义值的倒数计时器。
我想制作一个类似 google 的计时器: Google Timer
附:我是一个新的自学程序员。这是我的第二个 java 项目,所以我还没有太多经验。期待任何帮助:)
这是我的代码:
public class TestPane extends JFrame {
private LocalDateTime startTime;
private JLabel timerLabel;
private Timer timer;
private JButton startbtn;
private JTextField timeSet;
int count;
int count_2;
private Duration duration;
private JTextField minSet;
private JTextField secSet;
public TestPane() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(400,400);
getContentPane().setLayout(new CardLayout(0, 0));
JPanel panel = new JPanel();
getContentPane().add(panel, "name_87856346254020");
panel.setLayout(null);
timerLabel = new JLabel("New label");
timerLabel.setBounds(59, 48, 194, 14);
panel.add(timerLabel);
startbtn = new JButton("New button");
startbtn.setBounds(124, 187, 89, 23);
panel.add(startbtn);
timeSet = new JTextField(1);
timeSet.setBounds(106, 85, 86, 20);
panel.add(timeSet);
timeSet.setColumns(10);
JLabel lblHours = new JLabel("Hours");
lblHours.setBounds(29, 88, 46, 14);
panel.add(lblHours);
JLabel lblMinss = new JLabel("Mins");
lblMinss.setBounds(29, 114, 46, 14);
panel.add(lblMinss);
JLabel lblSecs = new JLabel("Secs");
lblSecs.setBounds(29, 139, 46, 14);
panel.add(lblSecs);
minSet = new JTextField(60);
minSet.setBounds(75, 116, 86, 20);
panel.add(minSet);
minSet.setColumns(10);
secSet = new JTextField();
secSet.setBounds(75, 147, 86, 20);
panel.add(secSet);
secSet.setColumns(10);
startbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timeSet.getText().trim();
if(timeSet.getText().isEmpty()) {
duration = Duration.ofMinutes(count_min);
} else {
count_hour = Integer.parseInt(timeSet.getText());
duration = Duration.ofHours(count_hour);
}
minSet.getText().trim();
if(minSet.getText().isEmpty()) {
duration = Duration.ofHours(count_hour);
} else {
count_min = Integer.parseInt(minSet.getText());
duration = Duration.ofMinutes(count_min);
}
secSet.getText().trim();
if(secSet.getText().isEmpty() && minSet.getText().isEmpty()) {
duration = Duration.ofHours(count_hour);
}
else if(secSet.getText().isEmpty() && timeSet.getText().isEmpty()){
duration = Duration.ofMinutes(count_sec);
}
else {
count_sec = Integer.parseInt(secSet.getText());
duration = duration.ofSeconds(count_sec);
}
if (timer.isRunning()) {
timer.stop();
startTime = null;
startbtn.setText("Start");
}
else {
startTime = LocalDateTime.now();
timer.start();
startbtn.setText("Stop");
}
}
});
timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
LocalDateTime now = LocalDateTime.now();
Duration runningTime = Duration.between(startTime, now);
Duration timeLeft = duration.minus(runningTime);
if (timeLeft.isZero() || timeLeft.isNegative()) {
timeLeft = Duration.ZERO;
startbtn.doClick(); // Cheat
}
timerLabel.setText(format(timeLeft));
}
});
}
protected String format(Duration duration) {
long hours = duration.toHours();
long mins = duration.minusHours(hours).toMinutes();
long seconds = (duration.minusMinutes(mins).toMillis() / 1000) %60;
return String.format("%02dh %02dm %02ds", hours, mins, seconds);
}
public static void main(String args[]){
new TestPane();
}
}
更新: 到目前为止,当用户设置小时并将分钟和秒留空时,它会从小时开始倒计时。与秒相同。但是,我无法获得时间来做同样的事情。如果我把所有东西都留空,除了分钟,计时器什么也不做。另外,我想让计时器与小时、分钟和秒同时工作。截至目前,它一次只能与一个单元一起使用。
【问题讨论】:
-
你可以颠倒这个概念,所以你有一个开始时间和一个持续时间,让
Timer定期打勾,比如说半秒,然后检查计时器是否已经运行在指定期间。您可以通过从持续时间中减去运行时间来轻松生成倒计时时间 -
作为example - 请记住,大多数时间操作发生在毫秒级别,因此您需要将输入转换为毫秒。您可能还想看看How to use Swing Timers
-
添加一个稍微复杂一点的example
-
谢谢,我会查看链接。这是我在 Java 中的第二个项目,所以我想我需要从小处着手。
-
这个
Duration.ofHours(count);什么都不做