Java - Swing - Timer with TimeUnit.sleep

0

I'm using Swing in NetBeans, creating a simple math game (I find it more viable than a calculator) whose goal is to add the random numbers of the buttons until it equals a number between 1 and 10000, which would be shown randomly. I used for to try something cool, to do something like a simple timer to make the game more difficult. At the moment, taking the fact that Netbeans asked to add try , catch , does not show any error, however, jLabel lblGameTime is not counting time.

Here's my method (or rather, what I got to do, because I know I did it wrong, I just do not know where):

public void GameTime() {
    int seconds = 120;
        for (int i = seconds; i < 0; i++) {
            lblGameTime.setText(String.valueOf(i));
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException ex) {
            Logger.getLogger(tela1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
    
asked by anonymous 22.03.2016 / 20:46

1 answer

1

You can use a Swing Timer for this:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class ExemploTimer extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ExemploTimer frame = new ExemploTimer();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ExemploTimer() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblGameTime = new JLabel();
        lblGameTime.setBounds(10, 236, 239, 14);
        contentPane.add(lblGameTime);

        int intervaloMilissegundos = 1000;

        Runnable runner = new Runnable() {
            Timer timer;
            public void run() {
                ActionListener actionListener = new ActionListener() {
                    private int contador = 10;

                    @Override
                    public void actionPerformed(ActionEvent event) {
                        if (contador == 0) {
                            // para o timer
                            timer.stop();
                            lblGameTime.setText("Tempo esgotado!");
                        } else {
                            lblGameTime.setText(String.format("%d restantes.", contador));
                        }
                        contador--;
                    }
                };

                // executa o actionListener a cada 1000 ms
                timer = new Timer(intervaloMilissegundos, actionListener);
                timer.setInitialDelay(0);
                timer.start();
            }
        };

        EventQueue.invokeLater(runner);
    }
}

Abcs!

    
22.03.2016 / 21:26