2011-01-04 8 views
1

In J2ME habe ich ein Formular mit einem StringItem erstellt, das die aktuelle Uhrzeit anzeigt. Ich möchte jedoch, dass dieses StringItem jede Minute aktualisiert wird. Zuerst habe ich einen Thread.sleep (60000) versucht, aber dann wartet die ganze App. Ich denke, ich muss einen neuen Thread erstellen? Soll ich das benutzerdefinierte Formular erstellen, das die Thread-Klasse erweitert? Ist das in J2ME möglich?Wie man eine digitale Uhr auf einem Formular in Java zeigt ME

Meine Klasse ohne die Implementierung eines Thread:


    import java.util.Calendar; 
    import java.util.Date; 
    import javax.microedition.lcdui.Command; 
    import javax.microedition.lcdui.Form; 
    import javax.microedition.lcdui.StringItem; 

    public class PtcInputForm extends Form{ 
    public Command okCommand; 
    public StringItem clock; 

    public PtcInputForm(String title) { 
     super(title); 
     okCommand= new Command("OK", Command.OK, 9); 
     this.addCommand(okCommand); 
     showClock(); 

    } 
    public void showClock(){ 
     String time = getTime(); 
     clock = new StringItem("time:", time); 
     this.append(clock); 
    } 
    public void refreshClock(){ 
     this.clock.setText(this.getTime()); 
    } 
    private String getTime(){ 
     Calendar c    = Calendar.getInstance(); 
     c.setTime(new Date()); 
     String time    = addZero(c.get(Calendar.HOUR_OF_DAY),2) +":"+ addZero(c.get(Calendar.MINUTE),2)+":"+addZero(c.get(Calendar.SECOND),2); 
     return time; 
    } 
    private static String addZero(int i, int size) { 
     String s = "0000"+i; 
     return s.substring(s.length()-size, s.length()); 


    } 
} 
 

Antwort

1

Ich denke, es durch die Implementierung einer Runnable-Klasse durchgeführt werden kann.

PtcInputForm ptcInputForm = new ptcInputForm("mytitle"); 
Thread clockThread = new Thread(ptcInputForm); 
clockThread.start(); 


    import java.util.Calendar; 
    import java.util.Date; 
    import javax.microedition.lcdui.Command; 
    import javax.microedition.lcdui.Form; 
    import javax.microedition.lcdui.StringItem; 

    public class PtcInputForm extends Form implements Runnable{ 
    public Command okCommand; 
    public StringItem clock; 

    public PtcInputForm(String title) { 
     super(title); 
     okCommand= new Command("OK", Command.OK, 9); 
     this.addCommand(okCommand); 
     showClock(); 

    } 
    public void showClock(){ 
     String time = getTime(); 
     clock = new StringItem("time:", time); 
     this.append(clock); 
    } 
    public void refreshClock(){ 
     this.clock.setText(this.getTime()); 
    } 
    private String getTime(){ 
     Calendar c    = Calendar.getInstance(); 
     c.setTime(new Date()); 
     String time    = addZero(c.get(Calendar.HOUR_OF_DAY),2) +":"+ addZero(c.get(Calendar.MINUTE),2)+":"+addZero(c.get(Calendar.SECOND),2); 
     return time; 
    } 
    private static String addZero(int i, int size) { 
     String s = "0000"+i; 
     return s.substring(s.length()-size, s.length()); 


    } 
    public void run() { 
     while(true){ 
      this.refreshClock(); 
      try { 
       Thread.sleep(60000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 
} 

+0

sehr schön explantion: Diese wird dann später genannt. Danke, hsmit. – Bakhtiyor