Timer: stop() : Timer « javax.swing « Java by API






Timer: stop()

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

class MainClass extends JFrame {
  Timer timer;

  int counter;

  MainClass(String title) {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ActionListener a = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Counter = " + counter);

        if (++counter > 10) {
          timer.stop();
          System.exit(0);
        }
      }
    };

    timer = new Timer(300, a);
    timer.start();

    pack();
    setVisible(true);
  }

  public static void main(String[] args) {
    new MainClass("Timer Demo1");
  }
}

           
       








Related examples in the same category

1.new Timer(int value, ActionListener act)
2.Timer: start()