位置分页 Javafx css

Position pagination Javafx css

如何在分页内容保持居中的情况下将分页移动到右下方

<Pagination fx:id="paging" maxPageIndicatorCount="5" pageCount="0" prefHeight="1010.0" prefWidth="1150.0" StackPane.alignment="CENTER_RIGHT">

页面控件和页面信息右对齐

您可以使用此 CSS 将分页控件右对齐。

.pagination {
    -fx-page-information-alignment: right;
}
.pagination > .pagination-control > .control-box {
    -fx-alignment: bottom-right;
    -fx-padding: 0.416em 1.1em 0 0;
}

控件框 1.1em 的右填充是为右对齐页面信息留出空间(表示“x/N”的内容,其中 x 是当前页面,N 是最大页数)。

页面控件右对齐,没有页面信息

如果您根本不需要页面信息,那么您可以右对齐 0em 而不是 1.1em(或者只定义无填充,因为默认右填充为 0),并且设置 -fx-page-information-visible: false 而不是设置 -fx-page-information-alignment.

.pagination {
    -fx-page-information-visible: false;
}
.pagination > .pagination-control > .control-box {
    -fx-alignment: bottom-right;
}

页面内容居中

要使每个页面的内容居中,请将每个页面放在 StackPane 中,如示例中所示。

如何学习如何自己写这个

有关样式分页的信息可在以下位置找到:

  • CSS reference guide Pagination section.
  • modena.css 文件在您的 JavaFX 安装中找到,例如:
    • javafx-controls-17.0.0.1-mac.jar -> com/sun/javafx/scene/control/skin/modena/modena.css.

示例应用程序

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class PageAlign extends Application {
    private static final String CUSTOM_PAGINATION_CSS = """
            data:text/css,
            .pagination {
                -fx-background-color: lemonchiffon;
                -fx-page-information-alignment: right;
            }
            .pagination > .pagination-control > .control-box {
                -fx-alignment: bottom-right;
                -fx-padding: 0.416em 1.1em 0 0;
            }
            """;

    @Override
    public void start(Stage stage) {
        final Pagination pagination = new Pagination();
        pagination.getStylesheets().add(CUSTOM_PAGINATION_CSS);
        pagination.setPageFactory(this::generatePage);
        pagination.setMaxPageIndicatorCount(6);
        pagination.setPageCount(6);
        pagination.setPrefSize(250, 140);
        stage.setScene(new Scene(pagination));
        stage.show();
    }

    private StackPane generatePage(Integer idx) {
        StackPane page = new StackPane(
                new Label("Page Content " + (idx + 1))
        );
        page.setStyle("-fx-background-color: lightblue;");

        return page;
    }

    public static void main(String[] args) {
        launch(args);
    }
}