9

Galera to precisando de uma ajuda aqui quero fazer um timer com JFrame mais não to conseguindo fazer ele repetir até chegar a zero, e se deixo o setRepeats em true não consigo fazer ele parar quando chegar em 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 Resposta 1

10

Mudando para setRepeats(true);, você pode verificar se os segundos chegaram a zero e executar o método stop() para parar o timer.

Veja no exemplo:

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
  • 1
    Funcionou brother vlw tinha tentando de um milhao e maneiras diferentes ja kk
    – tissei
    14/02/2014 às 17:49

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .