绑定为什么不与转场一起使用 [英] Why aren't Bindings working with Transitions
问题描述
我有一个Circle
,它的centerX
属性绑定到标签的text
属性.这用于查看对象在屏幕上的位置.每当我在圆上应用过渡时,绑定似乎都停止工作.这是代码段.
I have a Circle
and its centerX
property is bound to the text
property of a label. This is for viewing the position of the object on screen. The binding seems to stop working whenever I apply a transition on the circle. This is a snippet from the code.
//ERRONEOUS PART OF CODE
Circle circle = new Circle(50, 20, 20);
Label posLabel = new Label();
//binding
StringBinding binding = new StringBinding(){
{bind(circle.centerXProperty());}
@Override public String computeValue(){
return Double.toString(circle.getCenterX());
}
};
posLabel.textProperty().bind(binding);
//translation
TranslateTransition transition = new TranslateTransition(Duration.seconds(3), circle);
transition.setByX(250);
transition.play();
我想知道为什么绑定不适用于转换,并且在可能的情况下,解决该问题的方法.
I would like to know why bindings don't work with transitions, and if possible, a workaround to the problem.
PS :一个完整的最小可重现示例:(我说绑定不起作用,因为标签值停留在50.0)
P.S. A complete minimum reproducible example: (I am saying that the binding doesn't work because the labels value is stuck at 50.0)
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.shape.Circle;
import javafx.animation.TranslateTransition;
import javafx.util.Duration;
import javafx.beans.binding.StringBinding;
public class TransitionError extends Application
{
public void start(Stage stage) throws Exception
{
//instantiating all objects
Circle circle = new Circle(50, 20, 20);
Label posLabel = new Label();
Group group = new Group(circle, posLabel);
Scene scene = new Scene(group, 300,100);
stage.setTitle("JavaFX Example");
stage.setFullScreen(true);
stage.setScene(scene);
stage.show();
//ERRONEOUS PART OF CODE
//binding
StringBinding binding = new StringBinding(){
{bind(circle.centerXProperty());}
@Override public String computeValue(){
return Double.toString(circle.getCenterX());
}
};
posLabel.textProperty().bind(binding);
//translation
TranslateTransition transition = new TranslateTransition(Duration.seconds(3), circle);
transition.setByX(250);
transition.play();
}
}
推荐答案
TranslateTransition
不会更改centerX
的值;它会更改translateX
的值.
The TranslateTransition
doesn't change the value of centerX
; it changes the value of translateX
.
所以您可以这样做:
StringBinding binding = new StringBinding(){
{bind(circle.centerXProperty(), circle.translateXProperty());}
@Override public String computeValue(){
return Double.toString(circle.getCenterX()+circle.getTranslateX());
}
};
或者您可以使用原始绑定并为centerX
属性设置动画(这可能是更可取的,因为您似乎依赖centerX
进行更改):
Or you could use your original binding and animate the centerX
property instead (this may be preferable, since you seem to be relying on centerX
to change):
// TranslateTransition transition = new TranslateTransition(Duration.seconds(3), circle);
// transition.setByX(250);
// transition.play();
Timeline timeline = new Timeline(new KeyFrame(
Duration.seconds(3),
new KeyValue(circle.centerXProperty(), circle.getCenterX()+250)
));
timeline.play();
这篇关于绑定为什么不与转场一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!