JavaFx Pane Button position

狂风中的少年 提交于 2020-01-16 19:32:52

问题


As a layout I have a Pane. How do I set the position of a button always in the right corner when windows size is changed?

Pane root = new Pane();

Button b = new Button("Button ");

b.setLayoutX(100);
b.setLayoutY(0);
root.getChildren().add(b);

回答1:


Pane is not a good fit for this kind of layout. You could use

  • StackPane: This will align every single child to a corner, center of an edge or the center though.

  • AnchorPane: By default this layout works the same as Pane, but if you set anchors, you can set the distance of a child from the top, left, right and/or bottom.

Example:

AnchorPane root = new AnchorPane();

Button b = new Button("Button ");

// place button in the top right corner
AnchorPane.setRightAnchor(b, 0d); // distance 0 from right side of 
AnchorPane.setTopAnchor(b, 0d); // distance 0 from top

root.getChildren().add(b);


来源:https://stackoverflow.com/questions/58699331/javafx-pane-button-position

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