我有以下代码:
Task<Integer> task = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
Calendar tajm = Calendar.getInstance();
int hour = tajm.get(Calendar.HOUR_OF_DAY);
int minutes = 0;
updateMessage(Integer.toString(hour));
URL resource = getClass().getResource("classico.mp3");
Media media = new Media(resource.toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
while (true) {
try {
Thread.sleep(60000);
tajm = Calendar.getInstance();
if (tajm.get(Calendar.HOUR_OF_DAY) != hour) {
hour = tajm.get(Calendar.HOUR_OF_DAY);
updateMessage(Integer.toString(hour));
System.out.println("Hour has changed to: " + hour);
mediaPlayer.play();
} else {
hour = tajm.get(Calendar.HOUR_OF_DAY);
System.out.println("Hour has not changed, it's still: " + hour);
updateMessage(Integer.toString(hour));
}
} catch (InterruptedException ie) {
//System.err.print("...");
}
}
}
};
这首歌只播放一次,在第一次换时。知道为什么吗?
我还有一个不相关的问题:我的 javafx 程序需要 20 秒才能启动这正常吗?这有点烦人;p 普通应用程序,只有 JavaSE 几乎立即启动。