2013-01-02 53 views
0

我正在處理報警應用程序。我正在使用SwingUtilities.invokeLater方法來調用我的用戶界面的貪睡功能。但是當我運行它時,我注意到SwingUtilities.invokeLater函數沒有被調用。請幫我解決這個問題invokeLater沒有被調用

public class IsTime { 
int hrs; 
int min; 
int sec; 
GregorianCalendar clk=new GregorianCalendar(); 
Calendar gtl= Calendar.getInstance(); 

    mp3 mix=new mp3(); 



    public void makeReady(int h,int m,int s,String ampm){ 

    Calendar c1=Calendar.getInstance(); 
    c1.set(Calendar.HOUR_OF_DAY,h); 
    c1.set(Calendar.MINUTE, m); 
    c1.set(Calendar.SECOND,s); 


    if("PM".equals(ampm)){ 
     c1.set(Calendar.AM_PM, Calendar.PM); 
     if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.PM){ 

      if(c1.after(Calendar.getInstance())){ 
       check(c1); 
      } 
      c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1); 

      check(c1); 

     } 
    } 
    if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.AM){ 
      if(c1.after(Calendar.getInstance())){ 
       check(c1); 
      } 

      c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1); 

      check(c1); 
     } 
    if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.PM){ 
     c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1); 
     check(c1); 
    } 
} 
public void check(Calendar ch){ 
     while(!(Calendar.getInstance().after(ch))){//this line checks if it is time to sound alarm 
     } 

    SwingUtilities.invokeLater(new Runnable(){//here is my invokelater method 

       public void run(){ 
        System.out.println("git to invoke"); 
       new Snooze(); 

       } 

    }); 
      mix.start(); 


} 
public void makeReady(int mis){ 
     Calendar sz=Calendar.getInstance(); 
     sz.set(Calendar.SECOND, 0); 
     sz.set(Calendar.MINUTE, Calendar.getInstance().get(Calendar.MINUTE)+mis); 
     check(sz); 
    } 


} 

回答

2

這條線是真的不執行「等待」的正確方法操作:

while (!Calendar.getInstance().after(ch)) {// this line checks if it is time to sound alarm 
    } 

而是做:

Thread.sleep(ch.getTimeInMillis()-System.currentTimeMillis()); 

(你應該確保它是正面的)。就目前而言,您的代碼將持續評估是否可能會殺死您的CPU。

對於你的invokeLater(),很難說沒有一個適當的完整實例(一個SSCCE)。嘗試在通話之前放置一個日誌,看看它是否在您的控制檯中彈出。