JavaFX:拖放绝对定位

JavaFX: Drag and Drop absolute positioning

我在 JavaFX 中实现了拖放 ImageView 功能。我遇到的问题是,当 ImageView 放在 StackPane 上时,它会对齐到左上角。有没有办法让 ImageView 出现在它被放下的确切位置(绝对定位)?

假设您已经设置 alignment property of the StackPane or the ImageView to TOP_LEFT you can set the margin 来定位节点:

StrackPane pane = ...
...
ImageView iv = ...
...
double posX = ...
double posY = ...
StackPane.setMargin(iv, new Insets(posY, 0, 0, posX));
pane.getChildren().add(iv);

但是,由于您似乎在寻找 children 的绝对定位,因此 Pane 似乎是更合适的布局。 (Pane 的 children 的 layoutXlayoutY 属性不会被忽略。)