javaFx学习之分页控件Pagination
原文鏈接:DOC-03-27 分頁控件(Pagination Control) | JavaFX中文資料
JavaFX程序中添加分頁(Pagination)控件。它會(huì)告訴你如何向程序中添加Pagination控件,管理其分頁項(xiàng),并且使用CSS樣式來改變控件中各元素的風(fēng)格
Pagination控件用于在被拆分為多個(gè)小部分的分頁內(nèi)容間進(jìn)行導(dǎo)航
在觸摸式設(shè)備上,Pagination控件可以用于在查看一篇文檔時(shí)翻頁或在不同的屏幕之間切換
注意頁索引從0開始。因此如果想讓第3頁被選中,那么你需要將currentPageIndexProperty設(shè)置為2。
pagination3控件的頁面都是空的,因?yàn)闆]有向其中添加內(nèi)容。
你無法直接向Pagination控件中添加任何內(nèi)容項(xiàng),因?yàn)樗枰O(shè)置一個(gè)頁面工廠(Page Factory)。使用Pagination類的setPageFactory方法來實(shí)現(xiàn)一個(gè)頁面工廠,這樣來定義頁面的內(nèi)容
PageFactory頁面工廠類型,頁面工廠類的作用就是在分頁組件的主視圖區(qū)域內(nèi)部展現(xiàn)頁面內(nèi)容,頁面內(nèi)容的生成就是也頁面工廠類的作用
實(shí)現(xiàn)頁面工廠(PageFactory)
setPageFactory方法用于為Pagination控件定義PageFactory實(shí)現(xiàn)內(nèi)容。應(yīng)用程序開發(fā)者需要?jiǎng)?chuàng)建一個(gè)回調(diào)方法并設(shè)置一個(gè)PageFactory內(nèi)容來使用這個(gè)回調(diào)方法。回調(diào)方法會(huì)在一個(gè)頁面被選中時(shí)觸發(fā)。它會(huì)加載并返回被選中頁面的內(nèi)容。如果當(dāng)前被選中的頁面索引不存在,則必須返回null值
例子:
在Pagination類的構(gòu)造方法中可以直接對(duì)頁數(shù)和被選中的頁進(jìn)行定義。另外,你也可以先創(chuàng)建一個(gè)Pagination控件,然后再通過setPageCount和setCurrentPageIndex方法來設(shè)置頁數(shù)和被選中頁索引。
createPage方法聲明了Pagination控件的內(nèi)容,并且在setPageFactory方法中將其作為Page Factory進(jìn)行了調(diào)用。在createPage方法中創(chuàng)建了超鏈接和對(duì)應(yīng)的標(biāo)簽內(nèi)容作為分頁顯示的主要內(nèi)容,并且將它們進(jìn)行了縱向排列,在元素之間設(shè)置了5像素的間隔。
?當(dāng)前實(shí)現(xiàn)的Pagination控件會(huì)在頁數(shù)超過10時(shí)顯示10個(gè)Page Indicator。如果要改變顯示的Page Indicator數(shù)目,可以使用Pagination類的setMaxPageIndicatorCount方法。將下面的代碼行添加到例中則會(huì)顯示7個(gè)Page Indicator:pagination.setMaxPageIndicatorCount(7)
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 PaginationSample extends Application {private Pagination pagination;//分頁組件的定義public static void main(String[] args) throws Exception {launch(args);}public int itemsPerPage() {return 8;//每頁顯示條目數(shù)} //自定義的頁面內(nèi)容創(chuàng)建渲染方法public VBox createPage(int pageIndex) {VBox box = new VBox(5);//創(chuàng)建一個(gè)垂直布局盒子對(duì)象間隙距離為5int page = pageIndex * itemsPerPage();//頁碼號(hào)*當(dāng)頁條目數(shù)量=當(dāng)前條目起始索引位置for (int i = page; i < page + itemsPerPage(); i++) {//當(dāng)起始于當(dāng)頁記錄索引位置<下頁記錄索引位置的時(shí)候VBox element = new VBox();//創(chuàng)建一個(gè)垂直盒子布局器對(duì)象Hyperlink link = new Hyperlink("Item " + (i+1));//創(chuàng)建一個(gè)超級(jí)鏈接對(duì)象link.setVisited(true);//顯示超級(jí)鏈接對(duì)象Label text = new Label("搜索結(jié)果"+ link.getText());element.getChildren().addAll(link, text);//垂直盒子布局器中添加鏈接和文本標(biāo)簽box.getChildren().add(element);//scene場景的根布局器中添加垂直盒子布局器對(duì)象}return box;}@Overridepublic void start(final Stage stage) throws Exception {pagination = new Pagination(28, 0);//創(chuàng)建一個(gè)分頁組件pagination.setStyle("-fx-border-color:red;");//給分頁組件添加樣式pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));//分頁組件設(shè)置頁面?zhèn)鲄⒔换ソ壎?將頁面?zhèn)鱽淼膒ageIndex變量(頁碼選擇器)對(duì)應(yīng)的選擇值傳給頁面內(nèi)容創(chuàng)建渲染方法AnchorPane anchor = new AnchorPane();//創(chuàng)建錨點(diǎn)面板AnchorPane.setTopAnchor(pagination, 10.0);AnchorPane.setRightAnchor(pagination, 10.0);AnchorPane.setBottomAnchor(pagination, 10.0);AnchorPane.setLeftAnchor(pagination, 10.0);anchor.getChildren().addAll(pagination);//錨點(diǎn)布局面板中添加分頁組件pagination.setMaxPageIndicatorCount(7);//導(dǎo)航條一次最多展示7個(gè)頁碼導(dǎo)航Scene scene = new Scene(anchor);//場景對(duì)象上添加錨點(diǎn)布局面板stage.setScene(scene);//舞臺(tái)上添加場景stage.setTitle("標(biāo)題");//舞臺(tái)標(biāo)題stage.show();//舞臺(tái)展現(xiàn)} }————————————
展示了Pagination控件的另外一種用法。在這個(gè)程序中的每個(gè)頁面中都展示了一段文字。其中總共有5段文字,Pagination控件中聲明了28個(gè)頁面。為了避免出現(xiàn)ArrayIndexOutOfBoundsException,程序增加了頁面索引檢查,并且當(dāng)頁碼超過5時(shí)讓回調(diào)方法返回null值?
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 PaginationTest 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() {//每頁展示一行內(nèi)容return 1;}public VBox createPage(int pageIndex) {//創(chuàng)建分頁顯示區(qū)域中要顯示的頁面內(nèi)容VBox box = new VBox(5);//創(chuàng)建一個(gè)間隔距離為5的垂直盒子布局器組件int page = pageIndex * itemsPerPage();//求出當(dāng)前記錄的頁面上的索引起始值,當(dāng)前頁面碼值乘以每頁的條目數(shù)量for (int i = page; i < page + itemsPerPage(); i++) {TextArea text = new TextArea(textPages[i]);//頁面上的具體顯示內(nèi)容text.setWrapText(true);//設(shè)置文本環(huán)繞包圍效果box.getChildren().add(text);//垂直盒子布局器上添加文本框}return box;}@Overridepublic void start(final Stage stage) throws Exception {pagination = new Pagination(28, 0);//創(chuàng)建一個(gè)總頁數(shù)為28也的分頁控件,起始頁碼索引為0pagination.setStyle("-fx-border-color:red;");//設(shè)置分頁組件邊框顏色樣式pagination.setPageFactory((Integer pageIndex) -> {//頁面頁碼索引參數(shù)會(huì)自動(dòng)在恰當(dāng)時(shí)機(jī)傳進(jìn)分頁控件,這個(gè)是由交互進(jìn)行時(shí)態(tài)的控件運(yùn)行時(shí)自動(dòng)傳進(jìn)來的if (pageIndex >= textPages.length) {//當(dāng)當(dāng)前選中的頁碼數(shù)>文本頁內(nèi)容的條目數(shù)return null;//則返回展示的內(nèi)容為空} else {return createPage(pageIndex);//返回頁面要渲染展示的內(nèi)容}});AnchorPane anchor = new AnchorPane();//創(chuàng)建一個(gè)錨點(diǎn)布局面板對(duì)象AnchorPane.setTopAnchor(pagination, 10.0);AnchorPane.setRightAnchor(pagination, 10.0);AnchorPane.setBottomAnchor(pagination, 10.0);AnchorPane.setLeftAnchor(pagination, 10.0);anchor.getChildren().addAll(pagination);//錨點(diǎn)布局器對(duì)象中添加頁面分頁控件對(duì)象Scene scene = new Scene(anchor, 400, 250);//創(chuàng)建場景對(duì)象場景對(duì)象上掛載錨點(diǎn)布局對(duì)象stage.setScene(scene);//舞臺(tái)上掛載場景對(duì)象stage.setTitle("分頁組件使用例子");stage.show();//舞臺(tái)展現(xiàn)} }重點(diǎn)來了:
有時(shí)你無法設(shè)置精確的內(nèi)容項(xiàng)數(shù),因此也就無法計(jì)算Pagination控件中的頁數(shù)。在這種情況下,你可以在Pagination對(duì)象的構(gòu)造方法中加入計(jì)算頁數(shù)的代碼。例輸出了一個(gè)體列表并且通過列表的長度除以每頁的行數(shù)計(jì)算頁數(shù)
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Pagination; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.stage.Stage;public class Test8 extends Application {private Pagination pagination;//分頁組件的定義String[] fonts = new String[]{};//展示的文本內(nèi)容的定義public static void main(String[] args) throws Exception {launch(args);}public int itemsPerPage() {//每頁展示固定的15行return 15;}public VBox createPage(int pageIndex) {VBox box = new VBox(5);//創(chuàng)建一個(gè)垂直布局盒子對(duì)象間隙距離為5 int page = pageIndex * itemsPerPage();//頁碼號(hào)*當(dāng)頁條目數(shù)量=當(dāng)前條目起始索引位置for (int i = page; i < page + itemsPerPage(); i++) {//循環(huán)構(gòu)建渲染出要展示的條目組件TextField font = new TextField(fonts[i]);//創(chuàng)建一個(gè)文本框組件box.getChildren().add(font);//垂直盒子布局對(duì)象中添加要顯示的內(nèi)容本例中添加的是文本框組件}return box;}@Overridepublic void start(final Stage stage) throws Exception { fonts = Font.getFamilies().toArray(fonts);//獲得系統(tǒng)字體數(shù)組并將名稱內(nèi)容賦值給前文定義的String[] fontspagination = new Pagination(fonts.length/itemsPerPage(), 0);//通過數(shù)據(jù)條目算出總頁數(shù)pagination.setStyle("-fx-border-color:red;");//給分頁組件添加 css樣式pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));//給分頁組件掛載待展示顯示的內(nèi)容工廠方法AnchorPane anchor = new AnchorPane();//創(chuàng)建錨點(diǎn)面板AnchorPane.setTopAnchor(pagination, 10.0);AnchorPane.setRightAnchor(pagination, 10.0);AnchorPane.setBottomAnchor(pagination, 10.0);AnchorPane.setLeftAnchor(pagination, 10.0);anchor.getChildren().addAll(pagination);//在錨點(diǎn)面板上添加分頁組件Scene scene = new Scene(anchor, 400, 450);//創(chuàng)建場景對(duì)象stage.setScene(scene);//舞臺(tái)上創(chuàng)建場景對(duì)象stage.setTitle("場景上添加標(biāo)題");stage.show();//舞臺(tái)展現(xiàn)} }——————————————————————
為為Pagination控件設(shè)置樣式
通過設(shè)置樣式類為STYLE_CLASS_BULLET,你可以將Pagination控件的Page Indicator自定義為子彈形狀,替代原來數(shù)字序號(hào)Page Indicator。此外你可以修改modena樣式表中Pagination的默認(rèn)樣式
修改后的Pagination控件樣式?
.pagination {-fx-border-color: #00FF00; }.pagination .page {-fx-background-color: #00FF00; }.pagination .pagination-control {-fx-background-color: #00FF00; }.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; } import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Pagination; import javafx.scene.control.TextField; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.stage.Stage;public class Test8 extends Application {private Pagination pagination;//分頁組件的定義String[] fonts = new String[]{};//展示的文本內(nèi)容的定義public static void main(String[] args) throws Exception {launch(args);}public int itemsPerPage() {//每頁展示固定的15行return 15;}public VBox createPage(int pageIndex) {VBox box = new VBox(5);//創(chuàng)建一個(gè)垂直布局盒子對(duì)象間隙距離為5int page = pageIndex * itemsPerPage();//頁碼號(hào)*當(dāng)頁條目數(shù)量=當(dāng)前條目起始索引位置for (int i = page; i < page + itemsPerPage(); i++) {//循環(huán)構(gòu)建渲染出要展示的條目組件TextField font = new TextField(fonts[i]);//創(chuàng)建一個(gè)文本框組件box.getChildren().add(font);//垂直盒子布局對(duì)象中添加要顯示的內(nèi)容本例中添加的是文本框組件}return box;}@Overridepublic void start(final Stage stage) throws Exception {fonts = Font.getFamilies().toArray(fonts);//獲得系統(tǒng)字體數(shù)組并將名稱內(nèi)容賦值給前文定義的String[] fontspagination = new Pagination(fonts.length/itemsPerPage(), 0);//通過數(shù)據(jù)條目算出總頁數(shù)pagination.setStyle("-fx-border-color:red;");//給分頁組件添加 css樣式pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));//給分頁組件掛載待展示顯示的內(nèi)容工廠方法AnchorPane anchor = new AnchorPane();//創(chuàng)建錨點(diǎn)面板AnchorPane.setTopAnchor(pagination, 10.0);AnchorPane.setRightAnchor(pagination, 10.0);AnchorPane.setBottomAnchor(pagination, 10.0);AnchorPane.setLeftAnchor(pagination, 10.0);anchor.getChildren().addAll(pagination);//在錨點(diǎn)面板上添加分頁組件Scene scene = new Scene(anchor, 400, 450);//創(chuàng)建場景對(duì)象stage.setScene(scene);//舞臺(tái)上創(chuàng)建場景對(duì)象stage.setTitle("場景上添加標(biāo)題"); scene.getStylesheets().add("/test8.css");stage.show();//舞臺(tái)展現(xiàn)} }除了前面所應(yīng)用的樣式之外,你還可以在程序中考慮使用如下的樣式來改變Pagination控件的外觀:
· -fx-max-page-indicator-count ——設(shè)置Page Indicator的最大數(shù)值。
· -fx-arrows-visible ——控制后一頁和前一頁箭頭按鈕的可見性,默認(rèn)為true。
· -fx-tooltip-visible ——控制Page Indicator提示信息框的可見性,默認(rèn)為true。
· -fx-page-information-visible ——控制頁面信息的可見性,默認(rèn)為true。
· -fx-page-information-alignment ——設(shè)置頁面信息的對(duì)齊方式
總結(jié)
以上是生活随笔為你收集整理的javaFx学习之分页控件Pagination的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给自己的软件制作注册码
- 下一篇: java创建access数据库_使用Ja