javafx gui中的闪烁标签

sac*_*ner 2 javafx

我想在javafx中让标签在0.1秒内闪烁.该文本显示在后台运行的ImageView gif之上.我将如何进行此操作,或者您对最佳方法有任何建议?

谢谢

fab*_*ian 5

使用时间线:

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();
Run Code Online (Sandbox Code Playgroud)


Ita*_*iha 5

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

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)