我想在javafx中创建动画
第一个窗格将显示,在几秒钟的第一个窗格中将淡出,第二个窗格将显示。我怎样才能做到这一点?
我在线程中尝试这个。
这是我的代码
@Override
public void run() {
changingPane();
}
Thread t;
public void changingPane() {
t = new Thread() {
@Override
public void run() {
for (;;) {
if(mainController.getOpenPane()==0)
{
mainController.nextPane();
}
else{
mainController.prevPane();
}
}
}
};
t.start();
}
答案 0 :(得分:1)
假设您有两个窗格,
private Pane FIRST_PANE = new Pane();
private Pane SECOND_PANE = new Pane();
并添加这些方法以淡入和淡出窗格,
淡入方法
private void fadeInPane(Pane pane) {
FadeTransition fadeIn = new FadeTransition(Duration.millis(2900), pane);
fadeIn.setFromValue(0);
fadeIn.setToValue(1);
fadeIn.setOnFinished(e -> fadeOutPane(pane));
fadeIn.play();
}
淡出方法
private void fadeOutPane(Pane pane) {
FadeTransition fadeOut = new FadeTransition(Duration.millis(1900), pane);
fadeOut.setFromValue(1);
fadeOut.setToValue(0);
fadeOut.play();
}
然后根据您的逻辑和需要调用这些方法,
private void animatePane() {
boolean first_active = true;
Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
if(first_active){
fadeInPane(FIRST_PANE);
first_active = false;
}else{
first_active = true;
fadeInPane(SECOND_PANE);
}
}),
new KeyFrame(Duration.seconds(30))
);
clock.setCycleCount(Animation.INDEFINITE);
clock.play();
}