0

我正在开发一个警报应用程序。我正在使用 SwingUtilities.invokeLater 方法来调用我的 UI 以实现贪睡功能。但是当我运行它时,我注意到 SwingUtilities.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);  
    }  


}
4

1 回答 1

2

此行确实不是执行“等待”操作的正确方法:

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

而是这样做:

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

(你应该确保它是积极的)。就目前而言,您的代码将不断评估是否到了可能会杀死您的 CPU 的时间。

对于您的,如果没有适当的完整示例( SSCCEinvokeLater(),很难说清楚。尝试在通话之前放置一个日志,看看它是否会在您的控制台中弹出。

于 2013-01-02T15:35:29.840 回答