Java 单击按钮时计时器继续滴答作响,而不是重新启动
我正在开发一款问答应用程序游戏(上图截图)。每次用户单击“下一步”按钮时,我都希望计时器重新启动。不幸的是,这并没有发生,计时器一直在滴答作响。我不知道为什么。我可以试着解决这个问题吗Java 单击按钮时计时器继续滴答作响,而不是重新启动,java,swing,Java,Swing,我正在开发一款问答应用程序游戏(上图截图)。每次用户单击“下一步”按钮时,我都希望计时器重新启动。不幸的是,这并没有发生,计时器一直在滴答作响。我不知道为什么。我可以试着解决这个问题吗 final class Gui extends JFrame { private Timer timer; private JLabel timerLabel; private void createWindow() { display = new Display(); setLay
final class Gui extends JFrame {
private Timer timer;
private JLabel timerLabel;
private void createWindow() {
display = new Display();
setLayout(new BorderLayout(3, 3));
add(display, BorderLayout.CENTER);
JPanel timerPanel = new JPanel();
timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
timerPanel.setBackground(new Color(0x00bcda));
add(timerPanel, BorderLayout.PAGE_START);
timerLabel = new JLabel("01:00", SwingConstants.RIGHT);
timerLabel.setFont(new Font("Arial", Font.BOLD, 20));
timerLabel.setHorizontalAlignment(JLabel.RIGHT);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
timerLabel.setForeground(Color.black);
timerPanel.add(timerLabel, c);
timer = new Timer(1000, new ActionListener() {
int time = 60;
@Override
public void actionPerformed(ActionEvent e) {
time--;
timerLabel.setText(format(time / 60) + ":" + format(time % 60));
if (time == 0) {
timer = (Timer) e.getSource();
timer.stop();
}
}
});
}
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Quit")) {
System.exit(0);
} else if (cmd.equals("Start")) {
timer.start();
} else if (cmd.equals("Next")) {
timer.restart();
}
display.repaint();
}
}
}
重新启动计时器不会重新初始化
时间
字段
int time = 60; // here is your problem
要避免此问题,您需要重新创建计时器并重新启动它。例如,您可以使用单独的方法移动计时器初始化:
final class Gui extends JFrame {
private Timer timer;
private JLabel timerLabel;
private void createWindow() {
display = new Display();
setLayout(new BorderLayout(3, 3));
add(display, BorderLayout.CENTER);
JPanel timerPanel = new JPanel();
timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
timerPanel.setBackground(new Color(0x00bcda));
add(timerPanel, BorderLayout.PAGE_START);
timerLabel = new JLabel("01:00", SwingConstants.RIGHT);
timerLabel.setFont(new Font("Arial", Font.BOLD, 20));
timerLabel.setHorizontalAlignment(JLabel.RIGHT);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
timerLabel.setForeground(Color.black);
timerPanel.add(timerLabel, c);
initTimer();
}
private void initTimer() {
timer = new Timer(1000, new ActionListener() {
int time = 60;
@Override
public void actionPerformed(ActionEvent e) {
time--;
timerLabel.setText(format(time / 60) + ":" + format(time % 60));
if (time == 0) {
timer = (Timer) e.getSource();
timer.stop();
}
}
});
}
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Quit")) {
System.exit(0);
} else if (cmd.equals("Start")) {
timer.start();
} else if (cmd.equals("Next")) {
timer.stop();
initTimer();
timer.start();
}
display.repaint();
}
}
}
另一种可能是在
Gui
类中移动time
字段。在这种情况下,需要在计时器重新启动时重置此字段。“不工作”。。。你能详细说明一下吗?更新的问题-它更合适吗?不。我们没有看到任何按钮的声明或初始化,也没有看到您实际将ActionListener添加到按钮中。据我们所知,这是失踪的。谢谢这是伟大的。我要玩一玩。奇怪,这不管用。即使我按下了“下一步”按钮,计时器仍在滴答作响。我把密码放在这里:。@methuselah对不起,我没有时间查看你的密码。请用创建一个新问题,我将尽力帮助您。