Creating a timer with JAVA

9

I need a help here, I want to make a timer with JFrame but I can not get it to repeat until I get to zero, and if I leave setRepeats to true I can not make it stop when it reaches zero

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Eventos extends javax.swing.JFrame implements ActionListener{
int segundos = 30;
Timer timer;

public Eventos() {
    super("Eventos");
    this.timer = new Timer(1000,taskPerformer);
    this.timer.setCoalesce(true);
    this.timer.setRepeats(false);
    JButton botao = new JButton("Clique");
    botao.addActionListener(this);
    getContentPane().add(botao);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,300);
    setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
    while(segundos>0) this.timer.start();
}

ActionListener taskPerformer = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
      System.out.println(segundos--);
  }
  };

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

public static void main(String args[]) {
    new Eventos();

}
    
asked by anonymous 14.02.2014 / 18:19

1 answer

10

Changing to setRepeats(true); , you can check that the seconds have reached zero and execute the stop() method to stop the timer.

See in the example:

public class Eventos extends javax.swing.JFrame implements ActionListener{
    int segundos = 30;
    Timer timer;

    public Eventos() {
        super("Eventos");
        this.timer = new Timer(100,taskPerformer);
        this.timer.setCoalesce(true);
        this.timer.setRepeats(true);
        JButton botao = new JButton("Clique");
        botao.addActionListener(this);
        getContentPane().add(botao);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,300);
        setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        this.timer.start();
    }

    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            System.out.println(segundos);
            if (segundos == 0) {
                timer.stop();
            } else {
                segundos--;
            }
        }
    };

    public static void main(String args[]) {
        new Eventos();
    }
}
    
14.02.2014 / 18:34