javafx自定义分页控件的实现
由于javafx自带的分页控件太小,自己又不会改javafx分页控件的大小只能自己花了多半天的时间简单的写了一个分页控件,可实现选中的按钮一直再中间效果图如下:
话不多说代码如下:
package util; import javafx.scene.control.Button; import javafx.scene.layout.HBox; public class PagingUtil {//当前页private static int currentPage=1;//总页数private static int pageCount=10;//每页显示条数private static int showPageNumber=5;//上一页下一页按钮private static Button previousPage,nextPage;//存放页的HBoxprivate static HBox pageHBox=new HBox(20);//页数改变监听private static OnPageListener listener;/*** 刷新分页控件* @param pageCount 总页数*/public static void reset(int pageCount){currentPage=1;PagingUtil.pageCount=pageCount;setShowPage();}/*** 页数控件初始化* @return HBox*/public static HBox getPaging(){HBox hBox=new HBox(20);previousPage=new Button("上一页");previousPage.setStyle("-fx-background-radius: 10px");nextPage=new Button("下一页");nextPage.setStyle("-fx-background-radius: 10px");setShowPage();hBox.getChildren().addAll(previousPage,pageHBox,nextPage);//上一页下一页按钮的监听nextPage.setOnAction(np->{currentPage++;setShowPage();listener.update(currentPage);});previousPage.setOnAction(pp->{currentPage--;setShowPage();listener.update(currentPage);});return hBox;}/*** 改变页数*/private static void setShowPage(){pageHBox.getChildren().clear();if (currentPage==1){previousPage.setDisable(true);nextPage.setDisable(false);}else if(currentPage==pageCount){previousPage.setDisable(false);nextPage.setDisable(true);}else {previousPage.setDisable(false);nextPage.setDisable(false);}//判断是否是第一页并且只有一页if (currentPage-3<=0&&pageCount<=showPageNumber){//设置上一页和下一页不可点击for (int i = 0; i < pageCount; i++) {Button button=new Button((i+1)+"");//判断是否是当前页if(currentPage-i==1){button.setStyle("-fx-background-radius: 20px;-fx-background-color: #ffffcc");//设置当前页不可点击button.setDisable(true);}else{button.setStyle("-fx-background-radius: 20px;");}button.setOnAction(bu->{Button b= (Button) bu.getSource();currentPage=Integer.parseInt(b.getText());setShowPage();listener.update(currentPage);});pageHBox.getChildren().add(button);}}else if(currentPage-3<=0){for (int i = 0; i < showPageNumber; i++) {Button button=new Button((i+1)+"");//判断是否是当前页if(currentPage-i==1){button.setStyle("-fx-background-radius: 20px;-fx-background-color: #ffffcc");//设置当前页不可点击button.setDisable(true);}else{button.setStyle("-fx-background-radius: 20px;");}button.setOnAction(bu->{Button b= (Button) bu.getSource();currentPage=Integer.parseInt(b.getText());setShowPage();listener.update(currentPage);});pageHBox.getChildren().add(button);}}else if(currentPage+3>pageCount){//如果当前页加3大于总页数 则循环起点为pageCount-showPageNumber 终点为pageCountfor (int i = pageCount-showPageNumber; i < pageCount; i++) {Button button=new Button((i+1)+"");//判断是否是当前页if(currentPage-i==1){button.setStyle("-fx-background-radius: 20px;-fx-background-color: #ffffcc");//设置当前页不可点击button.setDisable(true);}else{button.setStyle("-fx-background-radius: 20px;");}button.setOnAction(bu->{Button b= (Button) bu.getSource();currentPage=Integer.parseInt(b.getText());setShowPage();listener.update(currentPage);});pageHBox.getChildren().add(button);}}else{//如果上面条件都不是则是正常页设置上下页都可以点击for (int i = currentPage-3; i < currentPage+2; i++) {Button button=new Button((i+1)+"");//判断是否是当前页if(currentPage-i==1){button.setStyle("-fx-background-radius: 20px;-fx-background-color: #ffffcc");//设置当前页不可点击button.setDisable(true);}else{button.setStyle("-fx-background-radius: 20px;");}button.setOnAction(bu->{Button b= (Button) bu.getSource();currentPage=Integer.parseInt(b.getText());setShowPage();listener.update(currentPage);});pageHBox.getChildren().add(button);}}}public interface OnPageListener{void update(int page);}public static void setOnPageListener(OnPageListener listener){PagingUtil.listener =listener;} }
使用的时候只需要直接调用即可返回HBox,然后实现监听页数改变的接口即可,代码如下:
package sample;import javafx.application.Application; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import util.PagingUtil;public class PagingDemo extends Application {@Overridepublic void start(Stage primaryStage) throws Exception {AnchorPane an=new AnchorPane();HBox hBox= PagingUtil.getPaging();Button button=new Button("button");an.getChildren().addAll(hBox,button);AnchorPane.setTopAnchor(button,100.0);Scene scene = new Scene(an);primaryStage.setScene(scene);primaryStage.setWidth(800);primaryStage.setHeight(800);primaryStage.show();PagingUtil.setOnPageListener(new PagingUtil.OnPageListener() {@Overridepublic void update(int page) {System.out.println(page);}});button.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {PagingUtil.reset(5);}});}public static void main(String[] args) {launch(args);} }
还有很多需要完善的地方,由于本人只需要每页显示5条,所以你们懂得哈哈哈哈!!有特殊需求的再自己完善吧!!
javafx自定义分页控件的实现相关推荐
- 也发一个自定义分页控件
分页控件有页面选择10 20 50显示,跳转到第几页,上页,下页功能.如图 代码为: using System; using System.Web.UI; using System.Web.UI.We ...
- .net 服务器端自定义分页控件 简单示例
使用效果如图: 先将控件添加到工具箱 将控件拖入到页面 会自动生成如下代码 <pager:pager ID="Pager1" runat="server" ...
- winform自定义分页控件
1.控件代码: public partial class PagerControl : UserControl {#region 构造函数public PagerControl() {Initiali ...
- 适用于ASP.NET的自定义分页控件
前段时间的项目中需要使用到分页控件,在网上找了不少,最终还是自己动手写一个,比较简单,后续功能以后再实现,现把代码及思路写下来,望大家批评指正.分页控件的分页形式大多分三种,一种是用URL传值,通过给 ...
- QT 自定义分页控件
Qt 自定义页码控件 一.效果展示 二.头文件 #ifndef PAGECONTROL_H #define PAGECONTROL_H #include <QList> #include ...
- 妹子UI之自定义分页控件
妹子UI,http://amazeui.org,在这儿顺便帮他打下广告,我个人觉得比Bootstrap漂亮,于是,我就选择了基于它的UI写出来自己的分页控件. paginator.js的内容如下: ( ...
- C#自定义用户控件之分页
形形色色的分页控件很多,有些可满足我们的要求,但有些却不一定,下面是我在工作当中遇到的问题而随手写了个能满足某些功能的自定义分页控件: 控件源代码: <%@ Control Language=& ...
- javaFx学习之分页控件Pagination
原文链接:DOC-03-27 分页控件(Pagination Control) | JavaFX中文资料 JavaFX程序中添加分页(Pagination)控件.它会告诉你如何向程序中添加Pagina ...
- [置顶] 分步实现具有分页功能的自定义DataList控件【附源代码】
一.控件也是类 [效果] [操作步骤] 1. 新建网站Web 2. 添加类CustomDataList.cs(系统会提示你把类建在App_Code文件夹中),代码如下: using System; ...
- 【开源】QuickPager ASP.NET2.0分页控件V2.0.0.3 【增加了使用说明】
================================ 欢迎转载,但是请注明出处.本文出自博客园 .谢谢合作! ================================ 最新版本:V ...
最新文章
- 阿里P9架构师讲解从单机至亿级流量大型网站系统架构的演进过程
- 运用多种知识点实现一个综合小游戏
- 20201125 《计算感知》武老师 第1节课 笔记
- 关于质粒,学生物的你应该知道的那些事儿
- VTK:网格之FitToHeightMap
- Vue表单类的父子组件数据传递示例_vue.js_脚本之家
- 常用于解决放缩问题的基本不等式及其几何直观证明
- 计算机一级表格样式在哪儿,word表格样式在哪 word表格样式在哪里
- CRT 之LISENCE
- UIAccelerometer 加速计
- MPI和OpenMP实现矩阵相乘
- 大学的第一堂必修课:军训
- 机器学习基础:台大李宏毅的线性代数视频公开课
- 程序员搞什么副业好?
- 双活数据中心架构分析及优缺点
- 苹果6s照相快门声音设置_手机隐藏的10种快门启动方式,你都知道吗?
- 用python 编写一个简单的游戏
- 计算机xp bios密码设置方法,如何设置电脑的密码
- WIN10计算机用户怎么改名,win10怎么改名字_win10怎么改用户名字
- 解决表格边框不显示的问题