[英]Countdown timer, error in displaying the seconds into JLabel
I am having trouble in displaying the seconds into a label.我无法在标签中显示秒数。 Whenever i run the code, it should be "9 (minutes):59 (seconds)" but it don't go as planned instead it only show 9 minutes.每当我运行代码时,它应该是“9(分钟):59(秒)”,但它没有按计划进行,而是只显示 9 分钟。
Note: I just used one label to display the minutes-seconds timer.注意:我只使用了一个标签来显示分秒计时器。
Here is the code: I used method.这是代码:我使用了方法。
private void countDown() {
tmClock = new Timer();
NumberFormat nbFormat = new DecimalFormat("00");
if (minutes == 0 && seconds == 0) {
JOptionPane.showMessageDialog(null, "time's up!");
tmClock.cancel();
new Result().setVisible(true);
this.dispose();
} else if (seconds == 0) {
minutes--;
seconds = 59;
}
lblTime.setText(nbFormat.format(minutes) + ":" );
String format = nbFormat.format(seconds);
tmClock.schedule(new TimerTask() {
@Override
public void run() {
seconds--;
countDown();
}
}, 1000);
}
Many problems here:这里有很多问题:
javax.swing.Timer
, not java.util.Timer
这是一个 Swing 应用程序,执行此操作的线程安全方法是使用javax.swing.Timer
,而不是java.util.Timer
countDown()
within your TimerTask, and almost in a recursive fashion adding new TimerTasks incorrectly to the Timer object.您正在 TimerTask 中调用countDown()
,并且几乎以递归方式将新的 TimerTasks 错误地添加到 Timer 对象中。 Note that you shouldn't even be using java.util.TimerTask
since again, this is a Swing application.请注意,您甚至不应该使用java.util.TimerTask
因为这是一个 Swing 应用程序。Solution:解决方案:
eg,例如,
// declaration section, create the Swing Timer
private int timerDelay = 1000; // msecs
private Timer myTimer = new Timer(timerDelay, e -> countDown());
// perhaps in a constructor or button's ActionListener, start the Timer
myTimer.start();
private void countDown() {
// seconds--; // update seconds
if (minutes == 0 && seconds == 0) {
myTimer.stop(); // stop the Timer if time's up
JOptionPane.showMessageDialog(null, "time's up!");
new Result().setVisible(true);
this.dispose();
} else if (seconds == 0) {
minutes--;
seconds = 59;
} else {
seconds--;
}
// create label's text and update JLabel
String text = String.format("%02d:%02d", minutes, seconds);
lblTime.setText(text);
}
Working example:工作示例:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class TestTimer extends JPanel {
private static final String FORMAT_TXT = "%02d:%02d";
private int seconds = 0;
private int minutes = 10;
private JLabel lblTime = new JLabel(String.format(FORMAT_TXT, minutes, seconds));
private int timerDelay = 1000; // msecs
private Timer myTimer = new Timer(timerDelay, e -> countDown());
public TestTimer() {
lblTime.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(new BorderLayout());
add(lblTime, BorderLayout.PAGE_START);
add(new JButton(new AbstractAction("Start") {
@Override
public void actionPerformed(ActionEvent e) {
if (myTimer != null && !myTimer.isRunning()) {
myTimer.start();
}
}
}));
}
private void countDown() {
if (minutes == 0 && seconds == 0) {
if (myTimer != null && myTimer.isRunning()) {
myTimer.stop(); // stop the Timer if time's up
}
JOptionPane.showMessageDialog(this, "time's up!");
// new Result().setVisible(true);
// this.dispose();
} else if (seconds == 0) {
minutes--;
seconds = 59;
} else {
seconds--; // update seconds
}
// create label's text and update JLabel
String text = String.format("%02d:%02d", minutes, seconds);
lblTime.setText(text);
}
private static void createAndShowGui() {
TestTimer mainPanel = new TestTimer();
JFrame frame = new JFrame("Test Timer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.