Javafx 简明教程

JavaFX - Pagination

Pagination 控件是一个 UI 组件,允许用户在内容的不同页面之间导航。例如,我们可以使用此功能显示搜索结果、文章、图像或任何其他需要多页的内容类型。一个典型的分页控件有一个页面导航区域、上一页按钮和下一页按钮,如下图所示 −

The Pagination control is a UI component that allows the user to navigate through different pages of content. For example, we can use this feature to display search results, articles, images or any other type of content that requires multiple pages. A typical pagination control has a page navigation area, previous page button and next page button as shown in the figure below −

pagination

Creating a Pagination in JavaFX

在 JavaFX 中,分页控件是通过实例化名为 Pagination 的类创建的。此类属于包 javafx.scene.control 。此类的构造函数如下所示 −

In JavaFX, the pagination control is created by instantiating a class named Pagination. This class belongs to the package javafx.scene.control. Constructors of this class are listed below −

  1. Pagination() − It will generate a pagination control without page count and the default page index will be zero. However, we can also use this default constructor and set these properties later using the setPageCount() and setCurrentPageIndex() methods.

  2. Pagination(int pageCount) − It will create a pagination control with the specified number of pages.

  3. Pagination(int pageCount, int currentIndex) − It constructs a new Pagination control with the given number of pages and page index.

在 JavaFX 中创建分页时,我们的第一步是使用以上构造函数实例化 Pagination 类。接下来,定义一个布局窗格,例如通过将 Pagination 对象传递给其构造函数的 Vbox 或 Hbox。最后,设置场景和舞台在屏幕上显示分页控件。

While creating a pagination in JavaFX, our first step would be instantiating the Pagination class by using any of the above mentioned constructors. Next, define a layout pane, such as Vbox or Hbox by passing the Pagination object to its constructor. Lastly, set the scene and stage to display the pagination control on the screen.

Example

下面的 JavaFX 程序演示了如何在 JavaFX 应用程序中创建分页。将此代码保存到名为 PaginationJavafx.java 的文件中。

The following JavaFX program demonstrates how to create a Pagination within JavaFX application. Save this code in a file with the name PaginationJavafx.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.util.Callback;
import javafx.geometry.Pos;
public class PaginationJavafx extends Application {
   private Pagination paging;
   @Override
   public void start(Stage stage) {
      // instantiating Pagination class
      paging = new Pagination(5);
      // to change the page number
      paging.setPageFactory(new Callback <Integer, Node>() {
         @Override
         public Node call(Integer pageIndex) {
            return new Label("Page No." + (pageIndex + 1));
         }
      });
      // creating Vbox that will contain the pagination
	  VBox box = new VBox();
      box.getChildren().addAll(paging);
      box.setAlignment(Pos.CENTER);
      // creating scene and stage
      Scene scene = new Scene(box, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Pagination in JavaFX");
      stage.show();
   }
   // to launch the application
   public static void main(String[] args) {
      launch(args);
   }
}

要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls PaginationJavafx.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PaginationJavafx

当我们执行上述代码时,它将生成以下输出。

When we execute the above code, it will generate the following output.

pagination output

Defining Current Index Page

Pagination 类的其中一个构造函数接受两个参数,即页面数和当前索引。每当我们使用此构造函数时,它会生成一个包含指定页面数和当前所选索引号的分页控件。

One of the constructor of Pagination class accepts two parameters namely page count and current index. Whenever we use this contructor, it will generate a pagination control with the specified number of pages and the currently selected index number.

Example

在下面的示例中,我们创建一个当前选定的索引号为 3 的分页控件。将此代码保存到名为 JavafxPagination.java 的文件中。

In the following example, we are creating a pagination control with currently selected index number as 3. Save this code in a file with the name JavafxPagination.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.util.Callback;
import javafx.geometry.Pos;
public class JavafxPagination extends Application {
   private Pagination paging;
   @Override
   public void start(Stage stage) {
      // creating pagination with 10 pages and currently selected index as 3
      paging = new Pagination(10, 2);
      // to change the page number
      paging.setPageFactory(new Callback <Integer, Node>() {
         @Override
         public Node call(Integer pageIndex) {
            return new Label("Page No." + (pageIndex + 1));
         }
      });
      // creating Vbox that will contain the pagination
	  VBox box = new VBox();
      box.getChildren().addAll(paging);
      box.setAlignment(Pos.CENTER);
      // creating scene and stage
      Scene scene = new Scene(box, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Pagination in JavaFX");
      stage.show();
   }
   // to launch the application
   public static void main(String[] args) {
      launch(args);
   }
}

通过使用以下命令从命令提示符编译并执行保存的 Java 文件 −

Compile and execute the saved Java file from the command prompt by using the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxPagination.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxPagination

在执行上述代码时,它将生成以下输出。

On executing the above code, it will generate the following output.

pagination output2