如何使用关键帧更改 JavaFX 标签的文本填充?
How do you change a JavaFX Label's textFill with a KeyFrame?
所以我一般理解关键帧构造函数接受节点 属性 和所需的最终值。
例如:
new KeyFrame(Duration.seconds(2), new KeyValue(YourNode.layoutXProperty, 75));
但是Label.textFill不存在,getTextFill是一个getter,不是成员变量。有什么办法可以解决这个问题吗?
代码的工作方式如下:
new KeyFrame(Duration.seconds(2), new KeyValue(YourNode.textFill, Color.GREEN));
T
类型的 JavaFX 属性 xxx
的方法命名模式是:
public Property<T> xxxProperty() // returns the property itself
public T getXxx() // returns the value of the property
public void setXxx(T x) // sets the value of the property
因此,例如,对于 Label
从 Labeled
继承的 textFill
属性,有一个 public ObjectProperty<Paint> textFillProperty()
方法,返回实际的 属性.
所以你只需要
new KeyFrame(Duration.seconds(2), new KeyValue(label.textFillProperty(), Color.GREEN))
SSCCE:
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TextFillTransition extends Application {
@Override
public void start(Stage primaryStage) {
Label label = new Label("Transitioning the fill of a label");
label.setStyle("-fx-font-size: 24pt ;");
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0), new KeyValue(label.textFillProperty(), Color.RED)),
new KeyFrame(Duration.seconds(2), new KeyValue(label.textFillProperty(), Color.GREEN))
);
timeline.setAutoReverse(true);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
StackPane root = new StackPane(label);
root.setPadding(new Insets(12));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
所以我一般理解关键帧构造函数接受节点 属性 和所需的最终值。
例如:
new KeyFrame(Duration.seconds(2), new KeyValue(YourNode.layoutXProperty, 75));
但是Label.textFill不存在,getTextFill是一个getter,不是成员变量。有什么办法可以解决这个问题吗?
代码的工作方式如下:
new KeyFrame(Duration.seconds(2), new KeyValue(YourNode.textFill, Color.GREEN));
T
类型的 JavaFX 属性 xxx
的方法命名模式是:
public Property<T> xxxProperty() // returns the property itself
public T getXxx() // returns the value of the property
public void setXxx(T x) // sets the value of the property
因此,例如,对于 Label
从 Labeled
继承的 textFill
属性,有一个 public ObjectProperty<Paint> textFillProperty()
方法,返回实际的 属性.
所以你只需要
new KeyFrame(Duration.seconds(2), new KeyValue(label.textFillProperty(), Color.GREEN))
SSCCE:
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TextFillTransition extends Application {
@Override
public void start(Stage primaryStage) {
Label label = new Label("Transitioning the fill of a label");
label.setStyle("-fx-font-size: 24pt ;");
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0), new KeyValue(label.textFillProperty(), Color.RED)),
new KeyFrame(Duration.seconds(2), new KeyValue(label.textFillProperty(), Color.GREEN))
);
timeline.setAutoReverse(true);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
StackPane root = new StackPane(label);
root.setPadding(new Insets(12));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}