首页 > 解决方案 > JavaFX 标签的 FillTransition

问题描述

有没有办法可以为标签制作类似 FillTransition 的东西?

我基本上只是想以动画方式更改 Label 的文本颜色。

已经谢谢你了!

标签: javajavafx

解决方案


一个简单的解决方案是使用Text节点而不是Label. Text扩展Shape,因此可以与 a 一起使用FillTransition。另一方面,您失去了添加背景和省略号功能的可能性。

如果您想继续使用 a ,我建议使用 a 为属性设置Label动画。textFillTimeline

例子:

@Override
public void start(Stage primaryStage) throws Exception {
    Color fromColor = Color.BLACK;
    Color toColor = Color.ORANGE;

    Label label = new Label("hello world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

    Timeline timeline = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(label.textFillProperty(), fromColor)),
            new KeyFrame(Duration.seconds(10), new KeyValue(label.textFillProperty(), toColor))
    );
    timeline.play();
    Scene scene = new Scene(new StackPane(label));
    primaryStage.setScene(scene);
    primaryStage.show();
}

推荐阅读