-2

Here is the following code, which I would like to use:

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

It should pause the running, but it makes seemly nothing.

1 Answers1

1

"It should pause the running, but it makes seemly nothing."

  1. You set the initial delay to 0, so there's no delay

  2. You have jLabel19.setText("452"); called every tick. What change do you expect with a hard coded value?


Maybe you want to decrease the counter and set the text to the counter.

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();
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720