Java - Swing - Timeunit.Leep time counter

Asked

Viewed 577 times

0

I’m using Swing in Netbeans, creating a simple math game (thought more feasible than a calculator) whose goal is to add up 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, do something like a simple timer to make the game harder. So far, apart from the fact that Netbeans asked to add a try,catch, does not present any error, however, the jLabel lblGameTime not counting the time.

Here’s my Method (or rather, what I managed to do, because I know I did wrong, I just don’t 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);
        }
    }
}

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!

Browser other questions tagged

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