Contagem regressiva - Quiz matemático

1 resposta
M

Pessoal, procurando no fórum achei alguns códigos para Contagem regressiva e o que deu mais certo foi esse:

try{
     			for (int i = Início.segundos; i > 0; i--){
     				System.out.println(i + " segundos");
    				Thread.sleep(1000); // 1 segundo
    			}
    			dispose();
     		} catch (InterruptedException e){
    			System.out.println("Erro");
    		}

O problema é que quando euclico no confirmar da classe Início ele começa a contagem sem mesmo abrir a classe Frame. Poderiam me ajudar?

Segue as classes:

Início:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;


	

public class Início extends JFrame implements ActionListener {
   JButton btn = new JButton("Confirmar");
   JLabel lbl = new JLabel("Matmatika");
   JLabel lbl2 = new JLabel("Número de jogadas");
   JLabel lbl3 = new JLabel("Escolha a dificuldade");
   JCheckBox cbo = new JCheckBox("Matplicão");
   JCheckBox cbo2 = new JCheckBox("Divisão");
   JCheckBox cbo3 = new JCheckBox("Subtração");
   JCheckBox cbo4 = new JCheckBox("Adição");
   JTextField txt2 = new JTextField();
   JTextField txt = new JTextField();
   JRadioButton rbt = new JRadioButton("Fácil");
   JRadioButton rbt2 = new JRadioButton("Médio");
   JRadioButton rbt3 = new JRadioButton("Difíil");
   ButtonGroup grp = new ButtonGroup();
   
   static int nj, segundos;
   
    public Início(){
        setTitle("Piroca");
        setSize(450, 500);
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);  
		this.setResizable(false); 
        
        btn.setBounds(10, 60, 130, 60);
        add(btn);
        
        lbl.setBounds(185, 10, 90, 23);
        add(lbl);
        
        txt.setBounds(185, 380, 30, 23);
        add(txt);
        
        lbl2.setBounds(60, 380, 180, 23);
        add(lbl2);
        
        lbl3.setBounds(15, 190, 180, 23);
        add(lbl3);
        
        rbt.setBounds(15, 220, 180, 23);
        add(rbt);
        
        rbt2.setBounds(15, 250, 180, 23);
        add(rbt2);
        
        rbt3.setBounds(15, 280, 180, 23);
        add(rbt3);
        
        grp.add(rbt);
        grp.add(rbt2);
        grp.add(rbt3);
           
        
        btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (rbt.isSelected()){
					segundos = 30;
				}
				
				else if(rbt2.isSelected()){
					segundos = 20;
				}
				
				else if(rbt3.isSelected()){
					segundos = 10;
				}
				
				new Frame().setVisible(true);
				nj = Integer.parseInt(txt.getText());
				dispose();
			}
		});
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
           new Início().setVisible(true);
    }

	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

   
}

Frame:

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.*;

public class Frame extends JFrame implements ActionListener{
	JButton btn = new JButton();
	JTextField txt = new JTextField();
	JLabel lbl = new JLabel();
	JLabel lbl2 = new JLabel("*");
	JLabel lbl3 = new JLabel("3");
	JLabel lbl4 = new JLabel();
	JLabel img = new JLabel();
	
	JProgressBar bar = new JProgressBar();
	
	int n1 = (int) (Math.random() * 50);
	int n2 = (int) (Math.random() * 50);
	int verificar;
	int verifaux;
	static int acertos;
	
	int nj2;
	
	Frame(){
		setTitle("Responda");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(450, 500);
		setLayout(null);
		this.setLocationRelativeTo(null);  
		this.setResizable(false); 
		
		
		
		lbl.setBounds(150, 50, 60, 60);
		lbl.setFont(new Font("Arial", Font.PLAIN, 30));
		lbl.setText(Integer.toString(n1));
		add(lbl);
		
		lbl2.setBounds(200, 50, 60, 60);
		lbl2.setFont(new Font("Arial", Font.PLAIN, 30));
		lbl2.setForeground(Color.RED);
		add(lbl2);
		
		lbl3.setBounds(230, 50, 60, 60);
		lbl3.setFont(new Font("Arial", Font.PLAIN, 30));
		lbl3.setText(Integer.toString(n2));
		add(lbl3);
		
		lbl4.setBounds(110, 150, 400, 90);
		lbl4.setFont(new Font("Arial", Font.PLAIN, 30));
		add(lbl4);
		
		txt.setBounds(320, 420, 80, 23);
		add(txt);
		
		bar.setBounds(10, 420, 250, 23);
		bar.setMaximum(Início.segundos);
		bar.setMinimum(0);
		
		try{
			for (int i = Início.segundos; i > 0; i--){
				System.out.println(i + " segundos");
				Thread.sleep(1000); // 1 segundo
			}
			dispose();
		} catch (InterruptedException e){
			System.out.println("Erro");
		}

		add(bar);
		
		
				txt.addKeyListener(new KeyAdapter() {
					public void keyPressed(final KeyEvent e) {
						int key = e.getKeyCode();
						if (key == KeyEvent.VK_ENTER) {		
							
							verifaux=0;
							verifaux = Integer.parseInt(txt.getText());
							
							verificar = n1*n2;
							
							
							if (verifaux == verificar){
								lbl4.setIcon(new javax.swing.ImageIcon("src\\imagens\\a.jpg"));
								lbl4.setText("Acertô");
								acertos++;
							}
							
							else{
								lbl4.setIcon(new javax.swing.ImageIcon("src\\imagens\\errou.png"));
								lbl4.setText("Errrrrou");
							}
							
							nj2++;
							
							n1 = (int) (Math.random() * 50);
							lbl.setText(Integer.toString(n1));
							
							n2 = (int) (Math.random() * 50);
							lbl3.setText(Integer.toString(n2));
							
							txt.setText("");
							
							if(nj2==Início.nj){
								new Resul().setVisible(true);
								dispose();
							}
						}
					}
				});
	       
				
		
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			new Frame().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	

}

1 Resposta

AllefT

Você está iniciando a contagem junto com o Frame!
Transforme está contagem em um método e chame-o quando o botão confirmar for clicado.

OBS: Seja mais claro na escolha do título de suas discussões, assim fica mais fácil para a galera da comunidade saber do que se trata e te responder mais rápido! :smiley::wink:

Criado 22 de fevereiro de 2016
Ultima resposta 24 de fev. de 2016
Respostas 1
Participantes 2