以下是我想要使用的代码:
private int counter = 1000;
private int delay = 1000;
private Timer timer;
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
jLabel19.setText("452");
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start();
它应该暂停运行,但它似乎没什么。
答案 0 :(得分:1)
“它应该暂停运行,但它似乎没什么。”
您将初始延迟设置为0,因此没有延迟
你jLabel19.setText("452");
打了个电话。您对硬编码值的期望有何变化?
也许您想减少计数器并将文本设置为计数器。
private int counter = 1000;
private int delay = 1000;
private Timer timer;
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (counter == 0) {
((Timer)e.getSource()).stop();
} else {
counter--;
jLabel19.setText(String.valueOf(counter));
}
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start();