绑定为什么不与转场一起使用

编程入门 行业动态 更新时间:2024-10-26 05:19:59
本文介绍了绑定为什么不与转场一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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();

更多推荐

绑定为什么不与转场一起使用

本文发布于:2023-11-17 03:49:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608722.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转场   绑定   不与

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!