Explicitly positioning nodes in JavaFX

后端 未结 2 1172
长情又很酷
长情又很酷 2020-11-29 12:47

When I have clicked a button, it changes its position.

But when I move the mouse, the button comes back to the center of the scene, why?

I have the followi

2条回答
  •  遥遥无期
    2020-11-29 13:09

    Not having deeply investigated the current case, I see a difference when I use an AnchorPane instead of the StackPane to place the Button on.

    By changing the label text of the Button by the mouseMoved-Event the Pane is rendered (layout is requested). With a StackPane placing all it's children in the center of itself the Button's position is reset to the center of the Pane. When you have a look into the layoutChildren method of StackPane you'll see a call to resizeRelocate. So layoutX and layoutY are reset and the button moves back to the center position (or whatever you set the alignment of the StackPane to).

    So I think this is a correct behavior of the StackPane and I recommend to use another Pane, e.g. AnchorPane.

提交回复
热议问题