JavaFX GUI中的闪烁标签

4
我想让一个标签在JavaFX中以0.1秒的间隔闪烁。该文本出现在正在运行的ImageView GIF的顶部。我该如何实现这个功能,或者你有什么建议最好的方法是什么?
谢谢。
2个回答

10

@fabian的解决方案很好。然而,在这种情况下,您可以使用FadeTransition。它改变了节点的不透明度,并且非常适合您的用例。

FadeTransition fadeTransition = new FadeTransition(Duration.seconds(0.1), label);
fadeTransition.setFromValue(1.0);
fadeTransition.setToValue(0.0);
fadeTransition.setCycleCount(Animation.INDEFINITE);

MCVE

(MCVE)是指最小化、完整可重现的示例,用于描述编程问题并帮助其他人理解和复制该问题。
import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class LabelBlink extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label label = new Label("Blink");
        FadeTransition fadeTransition = new FadeTransition(Duration.seconds(0.1), label);
        fadeTransition.setFromValue(1.0);
        fadeTransition.setToValue(0.0);
        fadeTransition.setCycleCount(Animation.INDEFINITE);
        fadeTransition.play();
        Scene scene = new Scene(new StackPane(label));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch();
    }
}

7
使用时间轴:
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.05), evt -> label.setVisible(false)),
                                 new KeyFrame(Duration.seconds( 0.1), evt -> label.setVisible(true)));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接