---- Class with a timer ----

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Timer; public class SomeTimer { public Timer time; public SomeTimer() { time = new Timer(1000, new Tick()); time.start(); } //Не работает ActionListener но почему?? class Tick implements ActionListener { int i = 0; public void actionPerformed(ActionEvent e) { i += 1; System.out.println(i); //time.stop(); } } } 

---- Main class ----

 public class Main { public static void main(String[] args) { new SomeTimer(); } } 

At startup, nothing is displayed = (

    1 answer 1

    Try to put after new SomeTimer(); Thread.sleep(2000) , otherwise you do not have time to work tick :)

    • Thanks a lot, helped :) - somizz11