Java - Problemas y dudas Thread e interfaces

 
Vista:
Imágen de perfil de Rudd

Problemas y dudas Thread e interfaces

Publicado por Rudd (1 intervención) el 09/09/2014 20:18:14
Primero que nada buenos dias,este es mi problema, tengo que hacer un cronometro usando obligatoriamente estas clases:
clase entidad= aqui se guardan las variables horas, minutos ,segundos junto con sus getters and setter.
clase vista = donde se crea la interfaz grafica del cronometro y aqui se implementa la interface
interface interfaz, con este se debe conectar la vista con la clase logica
clase logica = aqui se realizaran los calculos delcronometro y aqui se implementara runnable y la interface
Bien , se supone que en la vista se llamaran a los metodos de logica para usar los threads en logica y y asi calcule cada segundo que pase, pero para imprimirlos en lainterfaz debo mandarselo por medio de la interface a laclase entidad y que la vista los muestre en la interfaz grafica, pero esto mandando un objeto de entidad mediante la interface, sin embargo no se como hacer eso , no tengo ni la menor idea de como hacer de como mandar un objeto entidad a traves de la interface y que me lo muestre en interfaz grafica, ademas de que los threads no me estan funcionando tampoco, en fin.. gracias por su atencion.

Y aqui esta mi codigo:

package threads;

public class Main {

public static void main(String[] args) {

Vista croni=new Vista();
}

}

package threads;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Vista extends JFrame implements Interfaz {

Logica logica = new Logica();
Entidad entidad = new Entidad();
JLabel tiempo;
JButton iniciar;
JButton detener;
JButton reanudar;
JButton reiniciar;

public Vista()
{
setTitle("Super Cronometro");
setSize( 400, 150 );
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setVisible(true);

tiempo = new JLabel( "00:00:00" );
tiempo.setFont( new Font( Font.SANS_SERIF, Font.BOLD,55));
tiempo.setHorizontalAlignment( JLabel.CENTER );
tiempo.setForeground( Color.BLACK );

add( tiempo, BorderLayout.NORTH);

final JButton iniciar = new JButton( "Iniciar" );
iniciar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
iniciar.setEnabled(false);
logica.iniciarCronometro();
}
});

JButton detener = new JButton( "Detener" );
detener.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
iniciar.setEnabled(true);
logica.detenerCronometro();
}
});

JButton reanudar = new JButton( "Reanudar" );
reanudar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){

}
});
JButton reiniciar = new JButton( "Reiniciar" );
reiniciar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){

}
});

JPanel panelBoton = new JPanel();
panelBoton.setLayout(new GridLayout(1,4,4,4));
panelBoton.add(iniciar);
panelBoton.add(detener);
panelBoton.add(reanudar);
panelBoton.add(reiniciar);
add(panelBoton, BorderLayout.CENTER);
}

public void segundero(Entidad logica) {

tiempo.setText(entidad.getHoras() +":"+
entidad.getMinutos() +":"+entidad.getSegundos());
}
}

package threads;

public interface Interfaz {

public void segundero(Entidad logica);

}


package threads;

public class Entidad {

private String segundos="00";
private String minutos="00";
private String horas="00";

public String getSegundos()
{
return segundos;
}

public void setSegundos(String segundos)
{
this.segundos = segundos;
}

public String getMinutos()
{
return minutos;
}

public void setMinutos(String minutos)
{
this.minutos=minutos;
}

public String getHoras()
{
return horas;
}

public void setHoras(String horas)
{
this.horas = horas;
}
}


package threads;

public class Logica implements Runnable,Interfaz{

boolean cronometroActivo;
Thread hilo;
int horas=0;
int min = 0;
int seg = 0;
String hora;
String minutos;
String segundos;

public void segundero(Entidad entidad) {

}

public void run() {


try
{

while(cronometroActivo)
{
System.out.println("a");
Thread.sleep(60000);
seg++;

if(seg == 60)
{
seg = 0;
min ++;

if(min == 60)
{
min=0;
horas++;
}
}

if(horas<10)
{
hora = "0"+ Integer.toString(horas);
}
else
{
hora = Integer.toString(horas);
}

if(min<10)
{
minutos = "0"+ Integer.toString(min);
}
else
{
segundos = Integer.toString(seg);
}
System.out.println(hora+":"+minutos+":"+segundos);

}

}catch(Exception e){
System.out.println(hora+":"+minutos+":"+segundos);
}
}

public void iniciarCronometro(){
cronometroActivo = true;
hilo = new Thread(new Logica());
//System.out.println("a");
hilo.start();
}

public void detenerCronometro()
{
cronometroActivo = false;
hora="0";
minutos="0";
segundos="0";
}

public void reiniciarCronometro()
{
hora="0";
minutos="0";
segundos="0";
cronometroActivo=true;
hilo = new Thread(this);
hilo.start();
}

public void pausarCronometro(){
cronometroActivo = false;
hilo = new Thread(this);
hilo.start();
}


}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Problemas y dudas Thread e interfaces

Publicado por Tom (1831 intervenciones) el 10/09/2014 13:02:41
Hay bastantes maneras distintas de conseguir eso.
Una de ellas es usar wait/notify.

Este ejemplillo hace algo que te vale

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package testing;
 
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class Main {
	/* */
	Main() {
		Controller c = new Controller();
		ObservableData id;
 
		new Thread(c).start();
 
		while(true) {
			id = c.getValue();
			System.out.println(id.getValue());
		}
	}
	/* */
	class Controller implements Runnable {
		private IntegerData data = new IntegerData(0);
		/* */
		void wakeup() {
			synchronized(data) {
				data.notify();
			}
		}
		/* */
		IntegerData getValue() {
			try {
				synchronized(data) {
					data.wait();
					data.setValue(data.getValue() + 1);
				}
			} catch(InterruptedException ex) {
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
			return data;
		}
		@Override
		public void run() {
			try {
				for(int i = 0; i < 100; i++) {
					Thread.sleep(1000);
					wakeup();
				}
			} catch(InterruptedException ex) {
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	}
	/* */
	interface ObservableData {
		int getValue();
	}
	/* */
	class IntegerData implements ObservableData {
		int v;
		/* */
		IntegerData(int v) {
			this.v = v;
		}
		/* */
		@Override
		public int getValue() {
			return v;
		}
		/* */
		void setValue(int i) {
			v = i;
		}
	}
	/* */
	public static void main(String args[]) {
		Main m = new Main();
	}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar