我正在尝试创建一个时钟程序,并且我是 javaFX 编程的新手,我的代码如下所示:
package clock;
import java.util.Calendar;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
/**
*
* @author Jaskey */
public class Clock extends Application {
/**
* @param args the command line arguments
*/
private ImageView secondHand;
private ImageView minuteHand;
private ImageView hourHand;
private Calendar realTime = Calendar.getInstance();
int hours = realTime.get(Calendar.HOUR_OF_DAY);
int minutes = realTime.get(Calendar.MINUTE);
int seconds = realTime.get(Calendar.SECOND);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Clock 0.03");
primaryStage.initStyle(StageStyle.TRANSPARENT);
Circle clock = new Circle(150,Color.DARKGRAY);
clock.setEffect(new Glow(0.6f));
clock.setEffect(new InnerShadow(15, Color.BEIGE));
clock.setEffect(new DropShadow());
Circle clockCentre = new Circle(7,Color.THISTLE);
clockCentre.setEffect(new Shadow());
clockCentre.setEffect(new BoxBlur());
minuteHand = new ImageView(new Image(Clock.class.getResourceAsStream("images/minuteHand.png")));
minuteHand.setFitHeight(370);
minuteHand.setFitWidth(370);
minuteHand.setOpacity(0.85f);
secondHand = new ImageView(new Image(Clock.class.getResourceAsStream("images/secondHand.png")));
secondHand.setFitHeight(370);
secondHand.setFitWidth(370);
secondHand.setOpacity(0.85f);
hourHand = new ImageView(new Image(Clock.class.getResourceAsStream("images/hourHand.png")));
hourHand.setFitHeight(370);
hourHand.setFitWidth(370);
hourHand.setOpacity(0.85f);
if(seconds == seconds){
secondHand.setRotate(seconds*6);
}
if(minutes == minutes){
minuteHand.setRotate(minutes*6);
}
if(hours == hours){
hourHand.setRotate(hours*30);
}
final Timeline timelineSeconds = new Timeline();
timelineSeconds.setCycleCount(Timeline.INDEFINITE);
timelineSeconds.setAutoReverse(false);
final KeyValue SecondHand = new KeyValue(secondHand.rotateProperty(), 360);
final KeyFrame seconds2 = new KeyFrame(Duration.seconds(60), SecondHand);
timelineSeconds.getKeyFrames().addAll(seconds2);
timelineSeconds.play();
final Timeline timelineMinutes = new Timeline();
timelineMinutes.setCycleCount(Timeline.INDEFINITE);
timelineMinutes.setAutoReverse(false);
final KeyValue MinuteHand = new KeyValue(minuteHand.rotateProperty(), 360);
final KeyFrame minutes2 = new KeyFrame(Duration.minutes(60), MinuteHand);
timelineMinutes.getKeyFrames().add(minutes2);
timelineMinutes.play();
final Timeline timelineHours = new Timeline();
timelineHours.setCycleCount(Timeline.INDEFINITE);
timelineHours.setAutoReverse(false);
final KeyValue HourHand = new KeyValue(hourHand.rotateProperty(), 360);
final KeyFrame hours2 = new KeyFrame(Duration.hours(12), HourHand);
timelineHours.getKeyFrames().add(hours2);
timelineHours.play();
StackPane root = new StackPane();
root.getChildren().addAll(clock, clockCentre, hourHand, minuteHand, secondHand);
Scene myScene = new Scene(root, 650, 450);
myScene.setFill(null);
primaryStage.setScene((myScene));
primaryStage.show();
}
}
时钟指针工作正常(只是不与当前时间同步),所以我添加了这部分代码来解决问题:
if(seconds == seconds){
secondHand.setRotate(seconds*6);
}
if(minutes == minutes){
minuteHand.setRotate(minutes*6);
}
if(hours == hours){
hourHand.setRotate(hours*30);
}
唯一的问题是每分钟都会导致秒针抖动,并且每小时都会导致分针和时针每半天抖动一次。我知道问题的解决方案在于每 59 秒刷新一次以上代码 - 这就是我需要帮助的地方 - 我如何让它每 59 秒刷新一次???!
谢谢...