我在這裏有一些代碼,就像我想要的那樣工作。所有這些都是我在代碼中確定的特定日期的秒數。我使用Thread.currentThread().sleep(1000);
來更新一個JLabel,並保留當前時間直到日期。問題在於JLabel不會像預期的那樣每秒刷新一次。有時它每2秒更新一次,其他時間需要10秒更新。我相信這跟我打電話給我的方法有關,但我不確定如何使它更有效率。Thread.sleep效率()
下面是調用線程內更新JLabel中的方法的主要方法:
public static void main(String args[])
{
initUI();
try
{
while(true)
{
Thread.currentThread().sleep(1000);
getTime();
}
} catch(Exception e){System.out.println("An error has occured...");}
}
這裏是由主要方法調用的方法調用的方法。這種方法最終將剩下的變量到3號的方法秒:
public static void getTime()
{
Calendar c = Calendar.getInstance();
// Gets abstract current time in ms
long now = c.getTimeInMillis();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// Gets current time in ms
long msPassed = now - c.getTimeInMillis();
// There are 86,400,000 milliseconds in a day
// Gets the seconds remaining in the day
long secRemaining = (86400000 - msPassed)/1000;
//-----------------------------------------------------//
// Creates a new calendar for today
Calendar cal = Calendar.getInstance();
int currentDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
// Creates a calendar for November 20th, 2016
Calendar aniv = new GregorianCalendar(2016,10,20);
aniv.set(Calendar.MONTH, 10);
aniv.set(Calendar.DAY_OF_MONTH, 20);
int aniversary = aniv.get(Calendar.DAY_OF_YEAR);
remaining = ((aniversary - currentDayOfYear) * 24 * 60 * 60) + secRemaining;
setTextOnScreen(remaining);
}
最後,這是改寫JLabel中的方法(通過上述方法調用):
public static void setTextOnScreen(long num)
{
text.setForeground(Color.GREEN);
text.setLocation((int)width/2 - 150, 50);
text.setFont(new Font("Monospaced", Font.BOLD, 48));
text.setSize(300,150);
text.setText("" + num);
panel.add(text);
}
我不是包括代碼的其餘部分,因爲它應該是不相關的,但如果你也想看到這一點,請讓我知道。
你的睡眠是2秒的javadoc –