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();
}
Ran brother vlw had trying a million different ways ja kk
– tissei