如果您观看以下测试,您会看到所有圆圈都在移动,而不仅仅是添加了一个新圆圈。它不会每次都发生。我认为只有当新孩子超出现有范围时。但是,当我添加另一个圈子时,无论我把圈子放在哪里,我如何才能得到它,以便它不会移动组和它的所有子项?
请注意,如果我不在 Group 上设置比例,它们将不会全部移动。所以这与设置比例有关。
import javafx.application.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.stage.*;
import java.util.*;
public class GroupTest extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
Pane pane = new Pane();
Group root = new Group();
// NOTE: removing these two setScale* lines stops the undesirable behavior
root.setScaleX(.2);
root.setScaleY(.2);
root.setTranslateX(100);
root.setTranslateY(100);
root.layoutXProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
System.out.println("root layout: " + root.getLayoutX() + ", " + root.getLayoutY());
}
});
root.getChildren().addListener(new ListChangeListener<Node>() {
@Override public void onChanged(Change<? extends Node> change) {
System.out.println("root: " + root.getBoundsInParent());
System.out.println("root: " + root.getBoundsInLocal());
System.out.println("root: " + root.getLayoutBounds());
System.out.println("root: " + root.getLayoutX() + ", " + root.getLayoutY());
}
});
pane.getChildren().add(root);
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
stage.show();
new Thread(() -> {
Random r = new Random();
try {
while (true) {
expand = expand * 1.1;
Thread.sleep(700);
Platform.runLater(() -> {
root.getChildren().add(new Circle(r.nextInt((int)(1000*expand)) - 500*expand, r.nextInt((int)(1000*expand)) - 500*expand, r.nextInt(50)+30));
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
static double expand = 1.0;
}