Problemas con un Thread
Tengo un programa muy simple que muestra la hora, se espera un tiempo con un hilo y la vuelve a mostrar, bueno, esa es la idea, pero el problema es que no entra en el run() y, por tanto, ni espera un tiempo ni vuelve a mostrar la hora, alguien me puede ayudar a encontrar el error.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Reloj extends Frame implements Runnable
{
private TextField t;
private Thread f1;
public Reloj()
{
super("Reloj");
setSize(400,200);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System.exit(0);}});
setLayout(null);
t=new TextField(150);
t.setEditable(false);
t.setBounds(80,80,250,30);
add(t);
fecha();
show();
}
public void start()
{
f1=new Thread(this,"");
f1.start();
}
public void run()
{
while(true)
{
try {f1.sleep(1000);}
catch(InterruptedException e) {
System.err.println("Exepcio " + e.toString());}
fecha();
}
}
public void fecha()
{
Date d = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
t.setText(" Hora: " + gc.get(Calendar.HOUR_OF_DAY)+
" Minuto: " + gc.get(Calendar.MINUTE)+
" Segundo: "+gc.get(Calendar.SECOND));
}
public static void main(String[] args)
{
Reloj rj = new Reloj();
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Reloj extends Frame implements Runnable
{
private TextField t;
private Thread f1;
public Reloj()
{
super("Reloj");
setSize(400,200);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System.exit(0);}});
setLayout(null);
t=new TextField(150);
t.setEditable(false);
t.setBounds(80,80,250,30);
add(t);
fecha();
show();
}
public void start()
{
f1=new Thread(this,"");
f1.start();
}
public void run()
{
while(true)
{
try {f1.sleep(1000);}
catch(InterruptedException e) {
System.err.println("Exepcio " + e.toString());}
fecha();
}
}
public void fecha()
{
Date d = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
t.setText(" Hora: " + gc.get(Calendar.HOUR_OF_DAY)+
" Minuto: " + gc.get(Calendar.MINUTE)+
" Segundo: "+gc.get(Calendar.SECOND));
}
public static void main(String[] args)
{
Reloj rj = new Reloj();
}
}
Ola, pues te cambie algunas cosillas, suerte
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Reloj extends Frame implements Runnable {
private TextField t;
private Thread f1 = null;
public Reloj()
{
super("Reloj");
setSize(400,200);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System.exit(0);}});
setLayout(null);
t=new TextField(150);
t.setEditable(false);
t.setBounds(80,80,250,30);
add(t);
start();
show();
}
public void start() {
if(f1 == null){
f1 = new Thread(this);
f1.start();
}
}
public void run() {
while(f1 != null) {
System.err.println("into");
try {
f1.sleep(1000);
}
catch(InterruptedException e) {
System.err.println("Exepcio " + e.toString());
}
fecha();
}
f1 = null;
}
public void fecha() {
Date d = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
t.setText(" Hora: " + gc.get(Calendar.HOUR_OF_DAY)+
" Minuto: " + gc.get(Calendar.MINUTE)+
" Segundo: "+gc.get(Calendar.SECOND));
}
public static void main(String[] args) {
new Reloj();
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Reloj extends Frame implements Runnable {
private TextField t;
private Thread f1 = null;
public Reloj()
{
super("Reloj");
setSize(400,200);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System.exit(0);}});
setLayout(null);
t=new TextField(150);
t.setEditable(false);
t.setBounds(80,80,250,30);
add(t);
start();
show();
}
public void start() {
if(f1 == null){
f1 = new Thread(this);
f1.start();
}
}
public void run() {
while(f1 != null) {
System.err.println("into");
try {
f1.sleep(1000);
}
catch(InterruptedException e) {
System.err.println("Exepcio " + e.toString());
}
fecha();
}
f1 = null;
}
public void fecha() {
Date d = new Date();
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
t.setText(" Hora: " + gc.get(Calendar.HOUR_OF_DAY)+
" Minuto: " + gc.get(Calendar.MINUTE)+
" Segundo: "+gc.get(Calendar.SECOND));
}
public static void main(String[] args) {
new Reloj();
}
}