2011-01-04 3 views
1

J2ME에서 현재 시간을 표시하는 StringItem을 사용하여 양식을 만들었습니다. 그러나 매 분마다이 StringItem을 업데이트해야합니다. 먼저 Thread.sleep (60000)을 시도했지만 전체 앱이 대기 중입니다. 새 스레드를 만들어야겠습니까? Thread 클래스를 확장하는 사용자 정의 Form을 작성해야합니까? J2ME에서 가능합니까? 스레드의 구현없이Java ME에서 양식에 디지털 시계를 표시하는 방법

내 클래스 :


    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()); 


    } 
} 
 

답변

1

나는이의 Runnable 클래스를 구현하여 수행 할 수 있다고 생각합니다. 이것은 나중에 호출됩니다

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

아주 좋은 explantion. 고마워. – Bakhtiyor

관련 문제