javafx AnchorPane
示例
AnchorPane是一种布局,允许将内容放置在距离其侧面特定距离的位置。
有4种设置方法和4种获取距离的方法AnchorPane。这些方法的第一个参数是childNode。设置器的第二个参数是Double要使用的值。该值可以null指示给定边没有约束。
在以下示例中,将节点放置在距边指定距离的位置。
该center区域也被调整,以保持从侧面指定的距离。调整窗口大小时,请观察行为。
public static void setBackgroundColor(Region region, Color color) {
// 变为50%不透明度
color = color.deriveColor(0, 1, 1, 0.5);
region.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)));
}
@Override
public void start(Stage primaryStage) {
Region right = new Region();
Region top = new Region();
Region left = new Region();
Region bottom = new Region();
Region center = new Region();
right.setPrefSize(50, 150);
top.setPrefSize(150, 50);
left.setPrefSize(50, 150);
bottom.setPrefSize(150, 50);
// 用不同的半透明颜色填充
setBackgroundColor(right, Color.RED);
setBackgroundColor(left, Color.LIME);
setBackgroundColor(top, Color.BLUE);
setBackgroundColor(bottom, Color.YELLOW);
setBackgroundColor(center, Color.BLACK);
// 设置边距
AnchorPane.setBottomAnchor(bottom, 50d);
AnchorPane.setTopAnchor(top, 50d);
AnchorPane.setLeftAnchor(left, 50d);
AnchorPane.setRightAnchor(right, 50d);
AnchorPane.setBottomAnchor(center, 50d);
AnchorPane.setTopAnchor(center, 50d);
AnchorPane.setLeftAnchor(center, 50d);
AnchorPane.setRightAnchor(center, 50d);
// 用指定的孩子创建AnchorPane
AnchorPane anchorPane = new AnchorPane(left, top, right, bottom, center);
Scene scene = new Scene(anchorPane, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}