Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将线绑定到节点的中心_Java_Javafx - Fatal编程技术网

Java 将线绑定到节点的中心

Java 将线绑定到节点的中心,java,javafx,Java,Javafx,我正在创建一个动画,其中两个圆由一条线连接。当节点(圆)移动时,我希望线绑定它们的中心。我试过什么 import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.beans.property.DoubleProperty; import javafx.

我正在创建一个动画,其中两个圆由一条线连接。当节点(圆)移动时,我希望线绑定它们的中心。我试过什么

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class BindIt extends Application {

    @Override
    public void start(Stage primaryStage) {
        DoubleProperty startX = new SimpleDoubleProperty(100);
        DoubleProperty startY = new SimpleDoubleProperty(100);
        DoubleProperty endX = new SimpleDoubleProperty(300);
        DoubleProperty endY = new SimpleDoubleProperty(300);
        Line line = new Line(100, 100, 300, 300);
        line.startXProperty().bind(startX);
        line.startYProperty().bind(startY);
        line.endXProperty().bind(endX);
        line.endYProperty().bind(endY);
        Circle c1 = new Circle(25);
        c1.setCenterX(100);
        c1.setCenterY(100);
        c1.centerXProperty().bind(startX);
        c1.centerYProperty().bind(startY);

        Circle c2 = new Circle(25);
        c2.setCenterX(300);
        c2.setCenterY(300);
        c2.centerXProperty().bind(endX);
        c2.centerYProperty().bind(endY);


        Group root = new Group();
        root.getChildren().add(line);
        root.getChildren().add(c1);
        root.getChildren().add(c2);

        Scene scene = new Scene(root, 500, 500);

        primaryStage.setTitle("Bind the line!");
        primaryStage.setScene(scene);
        final Timeline timeline = new Timeline();
        timeline.getKeyFrames().addAll(new KeyFrame(Duration.millis(0)), new KeyFrame(Duration.millis(2000), new KeyValue(c1.centerXProperty(), 200)));
        timeline.play();

        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
如果我用“bind”删除所有的线,我会使圆移动。但使用bind时,我会出现错误,即“无法绑定等” 我是说这些台词

line.startXProperty().bind(startX);
line.startYProperty().bind(startY);
line.endXProperty().bind(endX);
line.endYProperty().bind(endY);

c1.centerXProperty().bind(startX);
c1.centerYProperty().bind(startY);

c2.centerXProperty().bind(endX);
c2.centerYProperty().bind(endY);
没有这些行,代码就可以工作。但是绑定没有发生

有人能告诉我哪里做错了吗?

你得到了错误

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: Circle.centerX : A bound value cannot be set.

这就说明了一切。不能在时间轴中设置圆的中心X和Y。你应该使用双属性来代替中心(X/Y)属性。

我找到了答案,如果你绑定一个属性,它是因变量,所以不能更改,只能更改自变量。所以我改变了

c1.centerXProperty().bind(startX) 


成功了。谢谢大家的评论。

你得到了什么结果?有什么东西在移动吗?如何移动?@AndreiNikolaenko在没有“绑定”的行的情况下,节点会正确移动。但是行绑定不起作用。为什么要绑定
endX
endY
?您似乎根本没有更改它们的值。
new-KeyValue(c1.centerXProperty(),200)
--更改为
new-KeyValue(startX,200)
@ItachiUchiha在本例中,我不移动c2,但我可能移动两个节点。如果我用“bind”删除行,我的代码就正常了!是否可以删除一个节点以及绑定到该节点的所有元素?
startX.bind(c1.centerXProperty())