JavaFX: How to position a component/node?

后端 未结 3 1765
一个人的身影
一个人的身影 2021-01-13 06:29

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

For example, I want to position a button to a position that I desir

相关标签:
3条回答
  • 2021-01-13 06:43

    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).

    0 讨论(0)
  • 2021-01-13 06:56

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

    <StackPane>
        <Button translateY="-15" translateX="15"  StackPane.alignment="TOP_RIGHT"/>
    </StackPane>
    
    0 讨论(0)
  • 2021-01-13 06:58

    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.

    0 讨论(0)
提交回复
热议问题