-1
//I call the methods under the init
{
showdate();
showtime();
}
void showdate()
{
Date d = new Date();
SimpleDateFormat a = new SimpleDateFormat("YYYY-MM-dd");
date.setText(a.format(d));
}
void showtime()
{
new Timer(0, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Date d = new Date();
SimpleDateFormat a = new SimpleDateFormat("hh:mm:ss");
time.setText(a.format(d));
}
}).start();
}
此代碼佔用了我CPU的80%,我真的需要在我的窗體上顯示時間和日期。Java定時器消耗CPU
也許你應該讓計時器在調用你的方法之間等待一段時間? – bmargulies
謝謝,我會嘗試一下,讓它知道它是否有效。 –
延遲時間爲0是不現實的,並且除了刻錄CPU週期外沒有其他用途。嘗試一個更實際的值,例如13或15,如果程序功能正常,則更長。 –