Creating timer with JAVA

Asked

Viewed 5,562 times

9

Guys in need of a help here want to make a timer with Jframe more not being able to make it repeat until it reaches zero, and if I leave the setRepeats in true I can’t 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();

}

1 answer

10


Switching to setRepeats(true);, you can check if the seconds have reached zero and run the method stop() 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();
    }
}
  • 1

    Ran brother vlw had trying a million different ways ja kk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.