JavaFX: How to position a component/node?

前提是你 提交于 2020-01-29 14:22:48

问题


In JavaFX, is there something similar to setLayout(); or setBounds();?

For example, I want to positioning a button to a position that I desire.


回答1:


Everything on JavaFX scene graph is a Node. Each node has a X-coordinate and a Y-coordinate. But there are different ways to set/change position of a child component. It depends on the layout manager used to show the component on the scene graph.

  • There are layout managers, like Group, which do not compute child's default position and you can use layoutX and layoutY directly on them
  • There are other layout managers, like Region, which automatically compute child's default position using layoutX and inorder to adjust the locations of the components from their default positions you need to use translateX and translateY values.

From the docs :

If the node is managed and has a Region as its parent, then the layout region will set layoutX according to its own layout policy. If the node is unmanaged or parented by a Group, then the application may set layoutX directly to position it.




回答2:


You should read up on the Node class (the long text at the beginning), and then especially relocate, setLayoutX (and Y) and setTranslateX (and Y).




回答3:


In addition to what others already mentioned , if you could place ur button ( or any node for that matter) inside a stack pane then u could make use of Stackpane's alignment property that takes javafx.geometry.Pos - The alignment of the child within the stackpane.For example in ur case:

<StackPane >
<Button translateY="-15" translateX="15"  StackPane.alignment="TOP_RIGHT"/>
</StackPane>


来源:https://stackoverflow.com/questions/29934183/javafx-how-to-position-a-component-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!