Flashing label in javafx gui

前端 未结 2 1787
情歌与酒
情歌与酒 2020-12-19 18:58

I\'m looking to make a label blink in and out ever 0.1 seconds in javafx. The text appears on top of an ImageView gif that is running in the background. How would I go about

相关标签:
2条回答
  • 2020-12-19 19:28

    Although @fabian's solution is nice, in this case, you could use a FadeTransition. It alters the opacity of the node and is a good fit for your case.

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

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-19 19:35

    Use a timeline:

    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();
    
    0 讨论(0)
提交回复
热议问题