我在点击时将矩形放在vbox上,然后打印矩形的坐标。按下键时我只打印坐标。请看这个示例代码示例:
@FXML
private VBox vbox;
@FXML
private AnchorPane anchorpane;
private List<Rectangle> rectangles = new ArrayList<>();
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("init");
}
@FXML
private void onMouseClick(MouseEvent e) {
System.out.println("click");
vbox.getChildren().clear();
for(int i = 0; i < 3 ; i++){
Rectangle r = new Rectangle(100.0, 10.0, Color.BLACK);
r.addEventHandler(KeyEvent.ANY, this::onKeyTyped);
rectangles.add(r);
vbox.getChildren().add(r);
System.out.println(" r Yposition :" + r.getLayoutY() + " or " + r.getBoundsInParent().getMinY() + " or " + r.getBoundsInLocal().getMinY());
System.out.println("Vbox height : " + vbox.getBoundsInLocal().getHeight());
}
}
@FXML
private void onKeyTyped(KeyEvent e) {
System.out.println("Key pressed");
for (Rectangle r : rectangles){
System.out.println(" r Yposition :" + r.getLayoutY() + " or " + r.getBoundsInParent().getMinY() + " or " + r.getBoundsInLocal().getMinY());
}
System.out.println("Vbox height : " + vbox.getBoundsInLocal().getHeight());
}
这给我以下输出:
click
r Yposition :0.0 or 0.0 or 0.0
Vbox height : 10.0
r Yposition :0.0 or 0.0 or 0.0
Vbox height : 10.0
r Yposition :0.0 or 0.0 or 0.0
Vbox height : 10.0
Key pressed
r Yposition :0.0 or 0.0 or 0.0
r Yposition :10.0 or 10.0 or 0.0
r Yposition :20.0 or 20.0 or 0.0
Vbox height : 30.0
所以我想JavaFX在屏幕上打印时会分配形状的坐标。但是当我刷新它时,如何在Vbox中获得形状的坐标?