首页 > 解决方案 > JavaFx 窗格按钮位置

问题描述

作为布局,我有一个Pane. 更改窗口大小时,如何将按钮的位置始终设置在右上角?

Pane root = new Pane();

Button b = new Button("Button ");

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

标签: javafxpane

解决方案


Pane不适合这种布局。你可以使用

  • StackPane:这会将每个孩子对齐到角落,边缘的中心或中心。

  • AnchorPane:默认情况下,此布局与 相同Pane,但如果设置锚点,则可以设置子元素与顶部、左侧、右侧和/或底部的距离。

例子:

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

推荐阅读