JavaFX布局-ScrollPane

  • 一个容器组件,用于展示那些可能超出窗口尺寸的内容
  • 当内容超过容器的大小时,会自动出现滚动条

常用属性

padding

内边距,可以单独设置上、下、左、右的内边距

scrollPane.setPadding(new Insets(10, 10, 10, 10));

content

容器内容,可以是任何布局容器

scrollPane.setContent(null);

vbarPolicy

垂直滚动条

scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);

hbarPolicy

水平滚动条

scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);

fitToWidth

是否ScrollPane的宽度将自动调整以适应其父容器的宽度

scrollPane.setFitToWidth(true);

fitToHeight

是否ScrollPane的高度将自动调整以适应其父容器的高度

scrollPane.setFitToHeight(true);

实现方式

Java实现

在这里插入图片描述

public static ScrollPane demo1() {
        ScrollPane scrollPane = new ScrollPane();
        // 内边距
        scrollPane.setPadding(new Insets(10, 10, 10, 10));
        // 垂直滚动条
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
        // 水平滚动条
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
        // 宽度自适应
        scrollPane.setFitToWidth(true);
        // 高度自适应
        scrollPane.setFitToHeight(true);

        FlowPane flowPane = new FlowPane();
        flowPane.setOrientation(Orientation.HORIZONTAL);
        flowPane.prefWidthProperty().bind(scrollPane.widthProperty().subtract(20));

        Circle circle = new Circle(300, Color.RED);
        Rectangle rectangle = new Rectangle(200, 150, Color.BLUE);
        Polygon polygon = new Polygon(10, 20, 30, 40, 50, 20);
        polygon.setFill(Color.RED);
        polygon.setStroke(Color.BLACK);
        polygon.setStrokeWidth(2);
        flowPane.getChildren().addAll(circle, rectangle, polygon);
        for (int i = 1; i < 100; i++) {
            flowPane.getChildren().add(new Button("Button " + i));
        }

        scrollPane.setContent(flowPane);
        return scrollPane;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值