问题
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 asPane
, 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