JavaFX UI控件教程(二十六)之Pagination Control

翻译自  Pagination Control

本章介绍如何向JavaFX应用程序添加分页控件。它教授如何向应用程序添加分页控件,管理其页面项,以及使用CSS样式设置控件元素的样式。

分页控件,用于浏览分成较小部分的多页内容。典型用途包括浏览邮箱中的电子邮件或在搜索结果中进行选择。在触摸设备中,分页控件可用于浏览文章的单个页面或在屏幕之间导航。图25-1显示了一个分页控件,它显示了操作系统中可用的字体。

图25-1分页控制

 

创建分页控件

分页控件由页面内容和页面导航区域组成。页面内容区域根据应用程序逻辑呈现和布置内容。页面导航区域包含预制控件,用于预览内容的特定部分。图25-2显示了导航区域的结构和基本元素。

图25-2分页控制的导航区域

您可以通过单击特定页面指示器或单击“下一页”和“上一页”按钮来浏览页面。选择第一页时,将禁用“上一页”按钮。同样,当选择最后一个导航指示器时,将禁用“下一页”按钮。

JavaFX SDK API提供了Pagination将分页控件添加到JavaFX应用程序的类。例25-1显示了Pagination该类的三个可用构造函数。

例25-1分页类的三个构造函数

//Creates a Pagination control with an INDETERMINATE page count 
//and the current page index equal to zero
pagination1 = new Pagination();
//Creates a Pagination control with 5 pages
//and the current page index equal to zero
pagination2 = new Pagination(5);
//Creates a Pagination control with 5 pages
//and the current selected index equal to 2
pagination3 = new Pagination(5, 2);

例25-1中的pagination3控件显示在图25-3中。

图25-3没有内容的分页控制

请注意,页面索引从0开始。因此,要从选择的第三个页面开始,您需要将其设置currentPageIndexProperty为2。

pagination3控件的页面为空,因为没有内容添加到控件。

您不能将任何项目直接添加到分页控件,它需要设置页面工厂。使用类的setPageFactory方法Pagination通过实现页面工厂来定义页面内容。

 

实施页面工厂

setPageFactory被用于定义分页控件的页面工厂。应用程序开发人员创建一个回调方法并将分页页面工厂设置为使用此回调。选择页面时将调用回调方法。它加载并返回所选页面的内容。null如果所选页面索引不存在,则必须返回该值。例25-2创建了一个包含28页的分页控件,并使用搜索结果填充页面,每页分配8个项目。

示例25-2将超链接添加到分页控件

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.Node;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;public class PaginationSample extends Application {private Pagination pagination;public static void main(String[] args) throws Exception {launch(args);}public int itemsPerPage() {return 8;}public VBox createPage(int pageIndex) {VBox box = new VBox(5);int page = pageIndex * itemsPerPage();for (int i = page; i < page + itemsPerPage(); i++) {VBox element = new VBox();Hyperlink link = new Hyperlink("Item " + (i+1));link.setVisited(true);Label text = new Label("Search results\nfor "+ link.getText());element.getChildren().addAll(link, text);box.getChildren().add(element);}return box;}@Overridepublic void start(final Stage stage) throws Exception {pagination = new Pagination(28, 0);pagination.setStyle("-fx-border-color:red;");pagination.setPageFactory(new Callback<Integer, Node>() {@Overridepublic Node call(Integer pageIndex) {return createPage(pageIndex);}});AnchorPane anchor = new AnchorPane();AnchorPane.setTopAnchor(pagination, 10.0);AnchorPane.setRightAnchor(pagination, 10.0);AnchorPane.setBottomAnchor(pagination, 10.0);AnchorPane.setLeftAnchor(pagination, 10.0);anchor.getChildren().addAll(pagination);Scene scene = new Scene(anchor);stage.setScene(scene);stage.setTitle("PaginationSample");stage.show();}
}

页面数和所选页面是在Pagination类的构造函数中定义的。或者,您可以Pagination使用setPageCountsetCurrentPageIndex方法创建控件并随后设置页数和所选页面的索引。

Pagination控件的内容在createPage用作页面工厂的方法中声明,并由setPageFactory方法调用。该createPage方法创建超链接对和相应的标签,并垂直排列,在元素之间设置五个像素的间隔。

编译并运行例25-2时,应该看到如图25-4所示的输出。

图25-4使用分页控件预览搜索结果

Pagination如果页数超过10 ,则控件的当前实现显示10个页面指示符。要为显示的页面指示符设置替代值,请使用类的setMaxPageIndicatorCount方法Pagination。例如,将以下行添加到示例25-2以显示七个页面指示符:pagination.setMaxPageIndicatorCount(7);。图25-5显示了应用更改后的PaginationSample应用程序。

图25-5更改页面指示符的数量

例25-3显示了分页控件的另一种用法。应用程序呈现文本片段,每页一个。片段的数量为5,并且分页控件的声明页数为28.要避免某种ArrayIndexOutOfBoundsException情况,请添加页面索引检查(在示例25-3中以粗体突出显示)并使回调方法在返回null时返回页数超过五。

示例25-3将文本片段添加到分页控件

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.Node;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;public class PaginationSample extends Application {private Pagination pagination;final String[] textPages = new String[]{"The apple is the pomaceous fruit of the apple tree, species Malus "+ "domestica in the rose family (Rosaceae). It is one of the most "+ "widely cultivated tree fruits, and the most widely known of "+ "the many members of genus Malus that are used by humans. "+ "The tree originated in Western Asia, where its wild ancestor, "+ "the Alma, is still found today.","The hawthorn is a large genus of shrubs and trees in the rose family,"+ "Rosaceae, native to temperate regions of the Northern Hemisphere "+ "in Europe, Asia and North America. The name hawthorn was "+ "originally applied to the species native to northern Europe, "+ "especially the Common Hawthorn C. monogyna, and the unmodified "+ "name is often so used in Britain and Ireland.","The ivy is a flowering plant in the grape family (Vitaceae) native to "+ " eastern Asia in Japan, Korea, and northern and eastern China. "+ "It is a deciduous woody vine growing to 30 m tall or more given "+ "suitable support,  attaching itself by means of numerous small "+ "branched tendrils tipped with sticky disks.","The quince is the sole member of the genus Cydonia and is native to "+ "warm-temperate southwest Asia in the Caucasus region. The "+ "immature fruit is green with dense grey-white pubescence, most "+ "of which rubs off before maturity in late autumn when the fruit "+ "changes color to yellow with hard, strongly perfumed flesh.","Aster (syn. Diplopappus Cass.) is a genus of flowering plants "+ "in the family Asteraceae. The genus once contained nearly 600 "+ "species in Eurasia and North America, but after morphologic "+ "and molecular research on the genus during the 1990s, it was "+ "decided that the North American species are better treated in a "+ "series of other related genera. After this split there are "+ "roughly 180 species within the genus, all but one being confined "+ "to Eurasia."};public static void main(String[] args) throws Exception {launch(args);}public int itemsPerPage() {return 1;}public VBox createPage(int pageIndex) {VBox box = new VBox(5);int page = pageIndex * itemsPerPage();for (int i = page; i < page + itemsPerPage(); i++) {TextArea text = new TextArea(textPages[i]);text.setWrapText(true);box.getChildren().add(text);}return box;}@Overridepublic void start(final Stage stage) throws Exception {pagination = new Pagination(28, 0);pagination.setStyle("-fx-border-color:red;");pagination.setPageFactory(new Callback<Integer, Node>() {@Overridepublic Node call(Integer pageIndex) {if (pageIndex >= textPages.length) {return null;} else {return createPage(pageIndex);}}});AnchorPane anchor = new AnchorPane();AnchorPane.setTopAnchor(pagination, 10.0);AnchorPane.setRightAnchor(pagination, 10.0);AnchorPane.setBottomAnchor(pagination, 10.0);AnchorPane.setLeftAnchor(pagination, 10.0);anchor.getChildren().addAll(pagination);Scene scene = new Scene(anchor, 400, 250);stage.setScene(scene);stage.setTitle("PaginationSample");stage.show();}
}

编译并运行例25-3时,您将看到如图25-6所示的输出。

图25-6在分页控件中渲染文本片段

在某些情况下,您无法设置要呈现的确切项目数,因此也无法设置分页控件中的页数。在这种情况下,您可以包含一行代码,用于计算Pagination对象构造函数中的页数。例25-4输出系统字体列表,并计算页数作为字体数组的长度除以每页的项数。

示例25-4添​​加未确定大小的内容

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;public class PaginationSample extends Application {private Pagination pagination;String[] fonts = new String[]{};public static void main(String[] args) throws Exception {launch(args);}public int itemsPerPage() {return 15;}public VBox createPage(int pageIndex) {        VBox box = new VBox(5);int page = pageIndex * itemsPerPage();for (int i = page; i < page + itemsPerPage(); i++) {Label font = new Label(fonts[i]);box.getChildren().add(font);}return box;}@Overridepublic void start(final Stage stage) throws Exception {fonts = Font.getFamilies().toArray(fonts);pagination = new Pagination(fonts.length/itemsPerPage(), 0);pagination.setStyle("-fx-border-color:red;");pagination.setPageFactory(new Callback<Integer, Node>() {@Overridepublic Node call(Integer pageIndex) {          return createPage(pageIndex);               }});AnchorPane anchor = new AnchorPane();AnchorPane.setTopAnchor(pagination, 10.0);AnchorPane.setRightAnchor(pagination, 10.0);AnchorPane.setBottomAnchor(pagination, 10.0);AnchorPane.setLeftAnchor(pagination, 10.0);anchor.getChildren().addAll(pagination);Scene scene = new Scene(anchor, 400, 450);stage.setScene(scene);stage.setTitle("PaginationSample");stage.show();}
}

编译并运行此示例时,它将生成如图25-7所示的应用程序窗口。

图25-7使用分页控件渲染系统字体

 

设计分页控制

您可以通过设置样式类来自定义分页控件以显示项目符号页面指示符而不是数字页面指示符STYLE_CLASS_BULLET。此外,您可以在caspian样式表中修改默认的分页样式。

例25-5给出了例25-4中分页控件的可视元素的一些替代样式。

例25-5分页控制的修改样式

.pagination {-fx-border-color:  #0E5D79;
}.pagination .page {-fx-background-color: #DDF1F8;
}.pagination .pagination-control {-fx-background-color: #C8C6C6;    
}.pagination .pagination-control .bullet-button {-fx-background-color: transparent, #DDF1F8, #0E5D79, white, white;
}.pagination .pagination-control .bullet-button:selected {   -fx-background-color: transparent, #DDF1F8, #0E5D79, white, #0E5D79;
}.pagination .pagination-control .left-arrow, .right-arrow{-fx-background-color: #DDF1F8, #0E5D79;
}

例25-6将这些样式应用于分页控件,并为页面指示器设置项目符号样式。

例25-6在PaginationSample应用程序中启用修改的分页控制样式

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;public class PaginationSample extends Application {private Pagination pagination;String[] fonts = new String[]{};public static void main(String[] args) throws Exception {launch(args);}public int itemsPerPage() {return 15;}public VBox createPage(int pageIndex) {        VBox box = new VBox(5);int page = pageIndex * itemsPerPage();for (int i = page; i < page + itemsPerPage(); i++) {Label font = new Label(fonts[i]);box.getChildren().add(font);}return box;}@Overridepublic void start(final Stage stage) throws Exception {fonts = Font.getFamilies().toArray(fonts);pagination = new Pagination(fonts.length/itemsPerPage(), 0);pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);pagination.setPageFactory(new Callback<Integer, Node>() {@Overridepublic Node call(Integer pageIndex) {          return createPage(pageIndex);               }});AnchorPane anchor = new AnchorPane();AnchorPane.setTopAnchor(pagination, 10.0);AnchorPane.setRightAnchor(pagination, 10.0);AnchorPane.setBottomAnchor(pagination, 10.0);AnchorPane.setLeftAnchor(pagination, 10.0);anchor.getChildren().addAll(pagination);Scene scene = new Scene(anchor, 400, 450);stage.setScene(scene);stage.setTitle("PaginationSample");scene.getStylesheets().add("paginationsample/ControlStyle.css");stage.show();}
}

将新定义的样式应用于PaginationSample应用程序并编译并运行它时,您将看到如图25-8所示的应用程序窗口。

图25-8带子弹页面指示符的PaginationSample和应用的新CSS样式

除了应用的样式,您还可以考虑以下样式来更改应用程序中分页控件的外观:

  • -fx-max-page-indicator-count - 设置页面指示符的最大数量。

  • -fx-arrows-visibletrue默认情况下,切换“下一个”和“上一个”按钮箭头的可见性。

  • -fx-tooltip-visibletrue默认情况下,切换页面指示器工具提示的可见性。

  • -fx-page-information-visibletrue默认情况下,切换页面信息的可见性。

  • -fx-page-information-alignment - 设置页面信息的对齐方式。

 

相关的API文档 

  • Pagination

  • VBox

  • AnchorPane

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/325003.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

长风破浪会有时,直挂云帆济沧海……

自从长假开学以来&#xff0c;班内学生的状态一直不如我所愿。前几天刚入学&#xff0c;因为马上就就业了&#xff0c;所以对他们的安排还是以做项目为主&#xff0c;早在假期的时候就已经将学生分为人数不等的8个小组&#xff0c;其中开发两个小组&#xff0c;前端5个小组以及…

ssl1759-求连通分量【图论,深搜,广搜】

题目 水题系列。给出一个图&#xff0c;求他的连通分量。 科普&#xff1a;连通分量就是一个图中可以连接最多点的子图&#xff08;可以是它本身&#xff09;的点数量。 输入 5(点的数量) 1 2(表示1和2连通) 3 4 2 3 0 0(表示停止输入) 输出 4 解题思路 Er…这道题没…

可变个数的形参

package com.wdl.day09;/** 可变个数形参的方法** 1.jdk 5.0新增的内容* 2.具体使用&#xff1a;* 2.1 可变个数形参的格式&#xff1a;数据类型 ... 变量名* 2.2 当调用可变个数形参的方法时&#xff0c;传入的参数个数可以是&#xff1a;0个&#xff0c;1个,2个&#xff…

JavaScript表单

JavaScript中表单知识总结 1 readonly和disabled 共同点&#xff1a;都是导致内容不可以更改 区别&#xff1a; readonly&#xff1a;中的内容是可以提交的 disabled&#xff1a;数据是不可以提交的,不可以在被操作 2表单提交的方式 οnsubmit“return sub()” <input t…

.NET 传奇 1.0 的出版过程,以及未来计划

今年五月初开始在微博的头条文章连载《.NET 的一点历史故事》&#xff0c;是一件非常快乐的事情。在各方朋友们的鼓励和支持之下&#xff0c;除去公开连载的八个章节&#xff0c;其后又先后完成其他十个章节&#xff0c;首先通过收费阅读渠道做了分享。到五月底的时候&#xff…

JavaFX UI控件教程(二十七)之File Chooser

翻译自 File Chooser 本章介绍如何使用FileChooser该类使用户能够导航文件系统。本章提供的示例说明了如何打开一个或多个文件&#xff0c;配置文件选择器对话框窗口以及保存应用程序内容。 与其他用户界面组件类不同&#xff0c;FileChooser该类不属于该javafx.scene.contr…

sql server高级查询,看这篇文章就够了

先选择一个数据库 use jobtest go引入&#xff1a;该数据库jobtest里面有两张表&#xff0c;Student学生表和Grade年级表&#xff0c;表中的数据如下所示&#xff1a; 学生表Student&#xff1a; 年级Grade表&#xff1a; 接下来我们来看看sql server中的子查询&#xff1a…

ssl1104-USACO 2.1城堡(foodfill)【图论,广搜】

##前言 由于这道题比较难&#xff0c;有不好描述&#xff0c;我就直接贴题目了。 ##Description 以一个几乎超乎想像的运气&#xff0c;农民约翰在他的生日收到了一张爱尔兰博彩的奖券。 这一张奖券成为了唯一中奖的奖券。 农民约翰嬴得爱尔兰的乡下地方的一个传说中的城堡。 …

直接输出数组的名字不一定是地址值

package com.wdl.day09;/*** 创建人 wdl* 创建时间 2021/8/12* 描述*/ public class ArrayPrintTest {public static void main(String[] args) {int[] arr new int[]{1, 2, 3};System.out.println(arr);//地址值char[] arr1 {a, b,c};System.out.println(arr1);//abc} }

jQuery的简介

jQuery的简介 [1]为什么学习jQuery A、JS书写代码的时候结构比较的臃肿 B、JS获得元素对象的方式比较的单一 C、JS书写的代码浏览器的兼容性比较差 D、JS实现动画的效果比较的麻烦 [2]什么是jQuery javascriptQuery(js库) 目前最流行的JavaScript函数库&#xff0c;对JavaScrip…

JavaFX UI控件教程(二十八)之UI控件的自定义

翻译自 Customization of UI Controls 本章介绍了UI控件自定义的各个方面&#xff0c;并总结了Oracle提供的一些提示和技巧&#xff0c;以帮助您修改UI控件的外观和行为。 您可以通过应用层叠样式表&#xff08;CSS&#xff09;&#xff0c;重新定义默认行为和使用单元工厂来…

好多人都说存储过程很难?认真看这篇文章就够了

何为存储过程&#xff1f;存储过程是在数据库管理系统中保存的、预先编译的并能实现某种功能的sql程序&#xff0c;说直白点&#xff0c;java知道吧&#xff1f;和java的方法一样。每遇到一个新的知识点时&#xff0c;我们都会看看它的优点&#xff0c;从而加深对它学习的欲望&…

下一代的 Actor 模型框架 Proto Actor

ProtoAct 是下一代的 Actor 模型框架&#xff0c;提供了 .NET 和 Go 语言的实现&#xff0c;默认支持分布式&#xff0c;提供管理和监控功能。在过去几年&#xff0c;我们经常看到两种 Actor 模型方法相互竞争&#xff0c;首先是经典的 Erlang/Akka 风格的 Actor 模型&#xff…

ssl1776-游乐场【图论,深搜】

题目 一个游乐场&#xff08;无向图&#xff09;&#xff0c;每个景点&#xff08;点&#xff09;都有一定的开心值&#xff08;价值&#xff09;&#xff0c;一个点只能游玩一次。然后可以从任何一个点出发&#xff0c;求可以游玩的最多项目&#xff08;连通分量&#xff09;…

JQuery $作用

作用1&#xff1a; $(function&#xff08;&#xff09;{}) $就是jQuery的省略写法 1.相当于 window.οnlοadfunction(){} 2.功能比window.onload更强大 1) window onload一个页面只能写一个,但是可以写多个$() 而不冲突 2) window onload要等整个页面加载完后再执行&#xff…

import关键字

在源文件中显式的使用import结构导入指定包下的类、接口 声明在包的声明和类的声明之间 如果需要导入多个结构&#xff0c;则并列写出即可 可以使用"xxx.*"的方式&#xff0c;表示可以导入xxx包下的所有结构 如果使用的类或接口是java.lang包下定义的&#xff0c…

信息学奥赛一本通1349-最优布线问题

【题目描述】 学校有n台计算机&#xff0c;为了方便数据传输&#xff0c;现要将它们用数据线连接起来。两台计算机被连接是指它们有数据线连接。由于计算机所处的位置不同&#xff0c;因此不同的两台计算机的连接费用往往是不同的。 当然&#xff0c;如果将任意两台计算机都用…

你知道i=i++;的含义吗?原理其实没有你想的那么简单

i和i我们都知道&#xff0c;但是你知道ii;吗&#xff1f;自上学时&#xff0c;老师就说i是先赋值再加&#xff0c;而i是先加再赋值&#xff0c;比如我们写个代码举下例子&#xff1a;i:/*** 自增案例*/public static void testZiZeng(){int i 1;int a i;System.out.println(&…

jQuery 的选择器

01、基本选择器 标签选择器 $(“a”) ID选择器 $(“#id”) $(“p#id”) 类选择器 $(“.class”) $(“h2.class”) 通配选择器 $("*") 并集选择器$(“elem1,elem2,elem3”) <script type"text/javascript">$(function(){/*******id选择器*********…

学会思考,而不只是编程

中国人常说“授之以鱼不如授之以渔”。如果说教授编程是授之以鱼&#xff0c;那么教授计算机科学就是授之以渔。为什么说学习计算机科学比学会编程要重要得多&#xff1f;来听听Yevgeniy Brikman的解释。 现如今&#xff0c;似乎每个人都在学习编程&#xff1a;Bill Gates、Ma…