Threads..dúvida

3 respostas
W

Preciso de ajuda de vcs..Tenho q fazer um exercício e não estou conseguindo.

/*Implementar um jogo de â??Clique rápidoâ??
	Janela possui 3 botões e dois label:
	Label 1: mostra o botão a ser clicado
	Label 2: mostra o tempo acumulado
	Quando label1 mostra um botão (p. ex., â??Botão 1â??), o usuário deve clicar no botão correspondente o mais rápido possível
	Número do botão e atraso é obtido aleatoriamente
	Tempo acumulado é mostrado no label2
	Jogo finaliza após cinco cliques
*/




package cliquerapido;


import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;


public class Clique {

	

	public static void main(String[] args) {
		
		MyFrame janela = new MyFrame();
                janela.setTitle("Clique Rápido");
		janela.setSize(400,200);
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		janela.setVisible(true);
	}
}


class MyFrame extends JFrame
{
	JLabel t1_status;
	JLabel t2_status;
	
	JLabel t1_clique;
        JLabel t1_tempo;

	JButton t1_up;
	JButton t1_down;
	JButton t1_center;

	
	MyFrame()
	{
		
		
		Container c = getContentPane();
		JPanel t1_panel = new JPanel();
		c.setLayout(new GridLayout(2,1));
		c.add(t1_panel);
		JPanel t1_status_panel = new JPanel();
		JPanel t1_op_panel = new JPanel();
		t1_panel.setLayout(new GridLayout(2,1));
		t1_panel.add(t1_status_panel);
		t1_panel.add(t1_op_panel);

		t1_status = new JLabel("Tempo: ");
		t2_status = new JLabel("Clique: ");
		t1_status_panel.add(t1_status);
		t1_op_panel.add(new JLabel("Clique: "));

		t1_tempo  = new JLabel(" ");
                t1_status_panel.add(t1_tempo);
		t1_clique = new JLabel(" ");
		t1_op_panel.add(t1_clique);
                
	
		
		t1_up = new JButton("1");
		t1_down = new JButton("2");
                t1_center = new JButton("3");
		t1_op_panel.add(t1_up);
		t1_op_panel.add(t1_down);
                t1_op_panel.add(t1_center);
                
                
                t1_up.addActionListener(new ThreadControl(this));
		t1_center.addActionListener(new ThreadControl(this));
                t1_down.addActionListener(new ThreadControl(this));
		
		
         }	

}

	

	
class ThreadControl implements ActionListener
{

	MyFrame ctrl_frame;
	int counter = 0;
        MyThread m1,m2,m3;
   
	ThreadControl(MyFrame mf)
	{
            
		ctrl_frame = mf;
               
                m1 = new MyThread(this,this.ctrl_frame.t1_center,1);
                m2 = new MyThread(this,this.ctrl_frame.t1_down,2);
                m3= new MyThread(this,this.ctrl_frame.t1_up,3);

		
                m1.start();
                m2.start();
                m3.start();
		
	}

    public void actionPerformed(ActionEvent e) {
             int numero = 1+(int)(Math.random() * 3);
             long init;   
             long end;   
             long diff;   
  
  
                init = System.currentTimeMillis();   
                /* Coloque aqui seu codigo que demora */  
               end = System.currentTimeMillis();   
               diff = end - init;   
               System.out.println("Demorou " + (diff / 1000) + " segundos");  
		
		if ((JButton)e.getSource() == ctrl_frame.t1_up)
		{
                        ctrl_frame.t1_tempo.setText(Double.toString(init));   
                        ctrl_frame.t1_clique.setText(Integer.toString(numero));

		}
		if ((JButton)e.getSource() == ctrl_frame.t1_down)
		{
                      ctrl_frame.t1_tempo.setText(Double.toString(init));   
                      ctrl_frame.t1_clique.setText(Integer.toString(numero));
			
		}
                
                if ((JButton)e.getSource() == ctrl_frame.t1_center)
		{
                      ctrl_frame.t1_tempo.setText(Double.toString(init));   
                      ctrl_frame.t1_clique.setText(Integer.toString(numero));
			
		}
    }
}

        
        class MyThread extends Thread
        {
	JButton status;
       
	ThreadControl th_ctrl;
	int th_id;
	int counter = 0;

         MyThread(ThreadControl tc, JButton l, int id) {

		status = l;
		th_id = id;
		th_ctrl = tc;
                
	

        }
	public void run()
	{
            
        }
}

3 Respostas

shoko

mas aonde está exatamente o seu problema?

W

Metodo run.

ViniGodoy

O problema é que o método está vazio.

Talvez esse post (ou os dois da página seguinte) podem ajuda-la a preenche-lo:
http://www.guj.com.br/posts/list/52964.java#279083

Criado 25 de julho de 2008
Ultima resposta 25 de jul. de 2008
Respostas 3
Participantes 3