【发布时间】:2020-03-01 02:49:56
【问题描述】:
作为布局,我有一个Pane。
更改窗口大小时如何设置按钮的位置始终在右上角?
Pane root = new Pane();
Button b = new Button("Button ");
b.setLayoutX(100);
b.setLayoutY(0);
root.getChildren().add(b);
【问题讨论】:
作为布局,我有一个Pane。
更改窗口大小时如何设置按钮的位置始终在右上角?
Pane root = new Pane();
Button b = new Button("Button ");
b.setLayoutX(100);
b.setLayoutY(0);
root.getChildren().add(b);
【问题讨论】:
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);
【讨论】: