gpt4 book ai didi

java - 倒计时器,在 JLabel 中显示秒数时出错

转载 作者:行者123 更新时间:2023-12-01 19:28:41 25 4
gpt4 key购买 nike

我在将秒显示到标签中时遇到问题。每当我运行代码时,它应该是“9(分钟):59(秒)”,但它没有按计划进行,而是只显示 9 分钟。

注意:我只是使用一个标签来显示分秒计时器。

代码如下:我用的方法。

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);

}

最佳答案

这里有很多问题:

  • 您使用了错误的 Timer 类。这是一个 Swing 应用程序,执行此操作的线程安全方法是使用 javax.swing.Timer,而不是 java.util.Timer
  • 您在 TimerTask 中调用 countDown(),并且几乎以递归方式将新的 TimerTasks 错误地添加到 Timer 对象中。请注意,您甚至不应该使用 java.util.TimerTask,因为这是一个 Swing 应用程序。

解决方案:

  • 使用 Swing Timer相反。
  • 在计时器的 ActionListener 中,更新秒值,然后设置 JLabel 的文本。

例如,

// 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);
}

工作示例:

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());
}
}

关于java - 倒计时器,在 JLabel 中显示秒数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60466196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com