百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

JavaFX 分页(java中分页实现步骤图解)

mhr18 2024-10-09 12:12 29 浏览 0 评论

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

创建分页控件

分页控件由页面内容和页面导航区域组成。

创建具有不确定页计数和当前页索引等于零的分页控件

Pagination pagination1 = new Pagination();

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

Pagination pagination2 = new Pagination(5);

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

Pagination pagination3 = new Pagination(5, 2);
/*
 * Copyright (c) 2008, 2014, Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the distribution.
 * - Neither the name of Oracle nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main 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;
 }
 @Override
 public void start(final Stage stage) throws Exception {
 pagination = new Pagination(28, 0);
 pagination.setStyle("-fx-border-color:red;");
 pagination.setPageFactory((Integer pageIndex) -> 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();
 }
}

上面的代码生成以下结果。

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

/*
 * Copyright (c) 2008, 2014, Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the distribution.
 * - Neither the name of Oracle nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
 
 private Pagination pagination;
 final String[] textPages = new String[]{
 "this is a test 1",
 "this is a test 2",
 "this is a test 3",
 "this is a test 4",
 "this is a test 5",
 "this is a test 6",
 "this is a test 7",
 "this is a test 8",
 "this is a test 8",
 };
 
 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;
 }
 
 @Override
 public void start(final Stage stage) throws Exception {
 pagination = new Pagination(28, 0);
 pagination.setPageFactory((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();
 }
}

上面的代码生成以下结果。

添加未确定大小的内容

/*
 * Copyright (c) 2008, 2014, Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the distribution.
 * - Neither the name of Oracle nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
 
public class Main 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;
 }
 
 @Override
 public void start(final Stage stage) throws Exception {
 fonts = Font.getFamilies().toArray(fonts);
 pagination = new Pagination(fonts.length/itemsPerPage(), 0);
 pagination.setPageFactory((Integer pageIndex) -> 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.show();
 }
}

上面的代码生成以下结果。

相关推荐

汉得信息:发布EBS系统安装启用JWS的高效解决方案

e公司讯,从汉得信息获悉,近日,微软官方宣布InternetExplorer桌面应用程序将于2022年6月15日正式停用。目前大部分客户都是使用IE浏览器打开EBS的Form界面,IE停用后,只能使...

36.9K star ! 推荐一个酷炫低代码开发平台!功能太强!

前言最近在逛github,看看能不能搜罗到一些对自己有帮助的开源软件。不经意间看到一个高star的java开源项目:jeecg-boot。进入在线演示版一看,感叹实在是太牛了!此开源项目不管是给来学习...

Linux新手入门系列:Linux下jdk安装配置

本系列文章是把作者刚接触和学习Linux时候的实操记录分享出来,内容主要包括Linux入门的一些理论概念知识、Web程序、mysql数据库的简单安装部署,希望能够帮到一些初学者,少走一些弯路。注意:L...

手把手教你在嵌入式设备中使用SQLite3

摘要:数据库是用来存储和管理数据的专用软件,使得管理数据更加安全,方便和高效。数据库对数据的管理的基本单位是表(table),在嵌入式linux中有时候它也需要用到数据库,听起来好难,其实就是几个函数...

JAVA语言基础(java语言基础知识)

一、计算机的基本概念什么是计算机?计算机(Computer)全称:电子计算机,俗称电脑。是一种能够按照程序运行、自动高速处理海量数据的现代化智能电子设备。由硬件和软件组成、没有安装过任何软件的计算机称...

再见 Navicat!一款开源的 Web 数据库管理工具!

大家好,我是Java陈序员。在日常的开发工作中,常常需要与各种数据库打交道。而为了提高工作效率,常常会使用一些可视化工具进行操作数据库。今天,给大家介绍一款开源的数据库管理工具,无需下载安装软件,基...

ORACLE数据库维护服务项目(oracle数据库日常维护)

招标资讯!ORACLE数据库维护服务项目转自:www.gxzbcg.cn/class2/208485.html...

盘点Java中最没用的知识④:这3个“过时选手”你还在教材里学?

一、JavaApplet:被浏览器“拉黑”的网页插件,为何还在教材里“苟延残喘”?你是不是在大学的Java入门课上,见过老师演示“网页上运行的Java小程序”?是不是好奇为什么现在打开银行旧版网银,...

甲骨文推出新的主权离线云产品,以提升安全性

据oracle网6月17日报道,Oracle宣布推出OracleComputeCloudCustomerIsolated,这是一种安全的主权计算云服务,可以与互联网断开连接,为政府和受监管行...

Domino容器化安装及运维笔记(容器化部署是什么意思)

1、容器作業系統選擇本案使用OracleLinux9.5最小化安裝作業系統安装中文语言包:yuminstallglibc-langpack-zh安装英文语言包:yuminstallglib...

Flink CDC基于流的数据集成工具(flink cdc hbase)

FlinkCDC(ChangeDataCapture)是基于流的数据集成工具:基本概述FlinkCDC是ApacheFlink生态中的重要组件,提供了一组源连接器,可使用变更数据捕获技...

王者归来(王者归来的下一句)

多年前运营了一个公众号,主要是写育儿和教育方面的,后因家庭成员增多,无力打理,故又很久未更新了。这两天,收到提示,提醒我账号需要登录,否则会被冻结,想想我还有三年就要顺利到站了,有20多年的IT运维实...

软件测试流程之数据库基础点(软件测试数据库知识)

扼要:1、了解当前流行的数据库有哪些;2、掌握基本的数据库命令;数据库,对测试人员来说,是必须要懂得的技能,不需要太精通,但要了解其大的概念、分类,掌握基本操作命令即可。甚至在一些公司中,测试人员还要...

什么是安全运维工程师(安全运维怎么样)

什么是安全运维工程师安全运维工程师是网络安全大方向下网络安全运行与维护岗的一个细分岗位。IT运维工作方向比较多,例如系统运维、应用运维、数据库运维、运维安全等。今天我们就来了解运维安全是指什么?怎么成...

成为运维大神要掌握哪些知识?终于有人说明白了!

在阅读文章前,辛苦您点下“关注”,方便讨论和分享。作者定会不负众望,按时按量创作出更优质的内容。要成为运维大神,需要掌握以下多方面的知识:一、操作系统知识1.Linux系统熟练掌握Linux...

取消回复欢迎 发表评论: