0

I recently made this program to count the seconds passing by but unfortunately, no print statement is showing. What is my problem here?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class MizohSoftware extends JFrame {

private class TimerListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        int time = 0;
        int inc = 1;
        time += inc;
        System.out.println(time + "seconds has passed");
    }
}

public void starttimer() {
    int delay = 1000;
    Timer display = new Timer(delay, new TimerListener());
    display.start();
}

public static void main(String[] args) {
    MizohSoftware MizohSoftware = new MizohSoftware();
    MizohSoftware.starttimer();
    }
}
2
  • Try sleeping the thread. Commented Jul 29, 2014 at 12:50
  • Just after you started your timer your program ends! It cannot display anything. Commented Jul 29, 2014 at 12:53

1 Answer 1

1

Your program ends before the timer event has a chance to fire.

E.g., add the following to your main:

  public static void main(String[] args) {
    MizohSoftware MizohSoftware = new MizohSoftware();
    MizohSoftware.starttimer();
    Thread.sleep(5000);
  }

This is probably not the behavior you want still; you need to read more about how the Java Swing event loop etc. works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.