分页控件用于浏览多个页面。 我们典型地使用对网页的分页控制,例如博客。 在博客页面的底部,我们可以看到一个矩形区域,作为一个数字列表来指示页面索引,以及一个下一个/上一个按钮来链接到下一个/上一个页面。

创建分页控件

分页控件由页面内容和页面导航区域组成。
创建具有不确定页计数和当前页索引等于零的分页控件

  1. Pagination pagination1 = new Pagination();

要创建一个5页的分页控件,当前页索引等于零

  1. Pagination pagination2 = new Pagination(5);

要创建一个5页的分页控件,当前所选索引等于2

  1. Pagination pagination3 = new Pagination(5, 2);
  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.control.Pagination;
  4. import javafx.scene.control.Hyperlink;
  5. import javafx.scene.control.Label;
  6. import javafx.scene.layout.AnchorPane;
  7. import javafx.scene.layout.VBox;
  8. import javafx.stage.Stage;
  9. public class Main extends Application {
  10. private Pagination pagination;
  11. public static void main(String[] args) throws Exception {
  12. launch(args);
  13. }
  14. public int itemsPerPage() {
  15. return 8;
  16. }
  17. public VBox createPage(int pageIndex) {
  18. VBox box = new VBox(5);
  19. int page = pageIndex * itemsPerPage();
  20. for (int i = page; i < page + itemsPerPage(); i++) {
  21. VBox element = new VBox();
  22. Hyperlink link = new Hyperlink("Item " + (i + 1));
  23. link.setVisited(true);
  24. Label text = new Label("Search results\nfor " + link.getText());
  25. element.getChildren().addAll(link, text);
  26. box.getChildren().add(element);
  27. }
  28. return box;
  29. }
  30. @Override
  31. public void start(final Stage stage) throws Exception {
  32. pagination = new Pagination(28, 0);
  33. pagination.setStyle("-fx-border-color:red;");
  34. pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
  35. AnchorPane anchor = new AnchorPane();
  36. AnchorPane.setTopAnchor(pagination, 10.0);
  37. AnchorPane.setRightAnchor(pagination, 10.0);
  38. AnchorPane.setBottomAnchor(pagination, 10.0);
  39. AnchorPane.setLeftAnchor(pagination, 10.0);
  40. anchor.getChildren().addAll(pagination);
  41. Scene scene = new Scene(anchor);
  42. stage.setScene(scene);
  43. stage.setTitle("PaginationSample");
  44. stage.show();
  45. }
  46. }

image.png

将分段文本添加到分页控件

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.control.Pagination;
  4. import javafx.scene.control.TextArea;
  5. import javafx.scene.layout.AnchorPane;
  6. import javafx.scene.layout.VBox;
  7. import javafx.stage.Stage;
  8. public class Main extends Application {
  9. private Pagination pagination;
  10. final String[] textPages = new String[]{
  11. "this is a test 1",
  12. "this is a test 2",
  13. "this is a test 3",
  14. "this is a test 4",
  15. "this is a test 5",
  16. "this is a test 6",
  17. "this is a test 7",
  18. "this is a test 8",
  19. "this is a test 8",
  20. };
  21. public static void main(String[] args) throws Exception {
  22. launch(args);
  23. }
  24. public int itemsPerPage() {
  25. return 1;
  26. }
  27. public VBox createPage(int pageIndex) {
  28. VBox box = new VBox(5);
  29. int page = pageIndex * itemsPerPage();
  30. for (int i = page; i < page + itemsPerPage(); i++) {
  31. TextArea text = new TextArea(textPages[i]);
  32. text.setWrapText(true);
  33. box.getChildren().add(text);
  34. }
  35. return box;
  36. }
  37. @Override
  38. public void start(final Stage stage) throws Exception {
  39. pagination = new Pagination(28, 0);
  40. pagination.setPageFactory((Integer pageIndex) -> {
  41. if (pageIndex >= textPages.length) {
  42. return null;
  43. } else {
  44. return createPage(pageIndex);
  45. }
  46. });
  47. AnchorPane anchor = new AnchorPane();
  48. AnchorPane.setTopAnchor(pagination, 10.0);
  49. AnchorPane.setRightAnchor(pagination, 10.0);
  50. AnchorPane.setBottomAnchor(pagination, 10.0);
  51. AnchorPane.setLeftAnchor(pagination, 10.0);
  52. anchor.getChildren().addAll(pagination);
  53. Scene scene = new Scene(anchor, 400, 250);
  54. stage.setScene(scene);
  55. stage.setTitle("PaginationSample");
  56. stage.show();
  57. }
  58. }

image.png

添加未确定大小的内容

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.control.Pagination;
  4. import javafx.scene.control.Label;
  5. import javafx.scene.layout.AnchorPane;
  6. import javafx.scene.layout.VBox;
  7. import javafx.scene.text.Font;
  8. import javafx.stage.Stage;
  9. public class Main extends Application {
  10. private Pagination pagination;
  11. String[] fonts = new String[]{};
  12. public static void main(String[] args) throws Exception {
  13. launch(args);
  14. }
  15. public int itemsPerPage() {
  16. return 15;
  17. }
  18. public VBox createPage(int pageIndex) {
  19. VBox box = new VBox(5);
  20. int page = pageIndex * itemsPerPage();
  21. for (int i = page; i < page + itemsPerPage(); i++) {
  22. Label font = new Label(fonts[i]);
  23. box.getChildren().add(font);
  24. }
  25. return box;
  26. }
  27. @Override
  28. public void start(final Stage stage) throws Exception {
  29. fonts = Font.getFamilies().toArray(fonts);
  30. pagination = new Pagination(fonts.length/itemsPerPage(), 0);
  31. pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
  32. AnchorPane anchor = new AnchorPane();
  33. AnchorPane.setTopAnchor(pagination, 10.0);
  34. AnchorPane.setRightAnchor(pagination, 10.0);
  35. AnchorPane.setBottomAnchor(pagination, 10.0);
  36. AnchorPane.setLeftAnchor(pagination, 10.0);
  37. anchor.getChildren().addAll(pagination);
  38. Scene scene = new Scene(anchor, 400, 450);
  39. stage.setScene(scene);
  40. stage.show();
  41. }
  42. }

image.png