javaFx学习之分页控件Pagination
原文鏈接:DOC-03-27 分頁控件(Pagination Control) | JavaFX中文資料
JavaFX程序中添加分頁(Pagination)控件。它會告訴你如何向程序中添加Pagination控件,管理其分頁項,并且使用CSS樣式來改變控件中各元素的風格
Pagination控件用于在被拆分為多個小部分的分頁內容間進行導航
在觸摸式設備上,Pagination控件可以用于在查看一篇文檔時翻頁或在不同的屏幕之間切換
注意頁索引從0開始。因此如果想讓第3頁被選中,那么你需要將currentPageIndexProperty設置為2。
pagination3控件的頁面都是空的,因為沒有向其中添加內容。
你無法直接向Pagination控件中添加任何內容項,因為它需要設置一個頁面工廠(Page Factory)。使用Pagination類的setPageFactory方法來實現一個頁面工廠,這樣來定義頁面的內容
PageFactory頁面工廠類型,頁面工廠類的作用就是在分頁組件的主視圖區域內部展現頁面內容,頁面內容的生成就是也頁面工廠類的作用
實現頁面工廠(PageFactory)
setPageFactory方法用于為Pagination控件定義PageFactory實現內容。應用程序開發者需要創建一個回調方法并設置一個PageFactory內容來使用這個回調方法。回調方法會在一個頁面被選中時觸發。它會加載并返回被選中頁面的內容。如果當前被選中的頁面索引不存在,則必須返回null值
例子:
在Pagination類的構造方法中可以直接對頁數和被選中的頁進行定義。另外,你也可以先創建一個Pagination控件,然后再通過setPageCount和setCurrentPageIndex方法來設置頁數和被選中頁索引。
createPage方法聲明了Pagination控件的內容,并且在setPageFactory方法中將其作為Page Factory進行了調用。在createPage方法中創建了超鏈接和對應的標簽內容作為分頁顯示的主要內容,并且將它們進行了縱向排列,在元素之間設置了5像素的間隔。
?當前實現的Pagination控件會在頁數超過10時顯示10個Page Indicator。如果要改變顯示的Page Indicator數目,可以使用Pagination類的setMaxPageIndicatorCount方法。將下面的代碼行添加到例中則會顯示7個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;//每頁顯示條目數} //自定義的頁面內容創建渲染方法public VBox createPage(int pageIndex) {VBox box = new VBox(5);//創建一個垂直布局盒子對象間隙距離為5int 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("搜索結果"+ link.getText());element.getChildren().addAll(link, text);//垂直盒子布局器中添加鏈接和文本標簽box.getChildren().add(element);//scene場景的根布局器中添加垂直盒子布局器對象}return box;}@Overridepublic void start(final Stage stage) throws Exception {pagination = new Pagination(28, 0);//創建一個分頁組件pagination.setStyle("-fx-border-color:red;");//給分頁組件添加樣式pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));//分頁組件設置頁面傳參交互綁定,將頁面傳來的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);//錨點布局面板中添加分頁組件pagination.setMaxPageIndicatorCount(7);//導航條一次最多展示7個頁碼導航Scene scene = new Scene(anchor);//場景對象上添加錨點布局面板stage.setScene(scene);//舞臺上添加場景stage.setTitle("標題");//舞臺標題stage.show();//舞臺展現} }————————————
展示了Pagination控件的另外一種用法。在這個程序中的每個頁面中都展示了一段文字。其中總共有5段文字,Pagination控件中聲明了28個頁面。為了避免出現ArrayIndexOutOfBoundsException,程序增加了頁面索引檢查,并且當頁碼超過5時讓回調方法返回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() {//每頁展示一行內容return 1;}public VBox createPage(int pageIndex) {//創建分頁顯示區域中要顯示的頁面內容VBox box = new VBox(5);//創建一個間隔距離為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);//創建一個總頁數為28也的分頁控件,起始頁碼索引為0pagination.setStyle("-fx-border-color:red;");//設置分頁組件邊框顏色樣式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("分頁組件使用例子");stage.show();//舞臺展現} }重點來了:
有時你無法設置精確的內容項數,因此也就無法計算Pagination控件中的頁數。在這種情況下,你可以在Pagination對象的構造方法中加入計算頁數的代碼。例輸出了一個體列表并且通過列表的長度除以每頁的行數計算頁數
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[]{};//展示的文本內容的定義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);//創建一個垂直布局盒子對象間隙距離為5 int page = pageIndex * itemsPerPage();//頁碼號*當頁條目數量=當前條目起始索引位置for (int i = page; i < page + itemsPerPage(); i++) {//循環構建渲染出要展示的條目組件TextField font = new TextField(fonts[i]);//創建一個文本框組件box.getChildren().add(font);//垂直盒子布局對象中添加要顯示的內容本例中添加的是文本框組件}return box;}@Overridepublic void start(final Stage stage) throws Exception { fonts = Font.getFamilies().toArray(fonts);//獲得系統字體數組并將名稱內容賦值給前文定義的String[] fontspagination = new Pagination(fonts.length/itemsPerPage(), 0);//通過數據條目算出總頁數pagination.setStyle("-fx-border-color:red;");//給分頁組件添加 css樣式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.setTitle("場景上添加標題");stage.show();//舞臺展現} }——————————————————————
為為Pagination控件設置樣式
通過設置樣式類為STYLE_CLASS_BULLET,你可以將Pagination控件的Page Indicator自定義為子彈形狀,替代原來數字序號Page Indicator。此外你可以修改modena樣式表中Pagination的默認樣式
修改后的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[]{};//展示的文本內容的定義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);//創建一個垂直布局盒子對象間隙距離為5int page = pageIndex * itemsPerPage();//頁碼號*當頁條目數量=當前條目起始索引位置for (int i = page; i < page + itemsPerPage(); i++) {//循環構建渲染出要展示的條目組件TextField font = new TextField(fonts[i]);//創建一個文本框組件box.getChildren().add(font);//垂直盒子布局對象中添加要顯示的內容本例中添加的是文本框組件}return box;}@Overridepublic void start(final Stage stage) throws Exception {fonts = Font.getFamilies().toArray(fonts);//獲得系統字體數組并將名稱內容賦值給前文定義的String[] fontspagination = new Pagination(fonts.length/itemsPerPage(), 0);//通過數據條目算出總頁數pagination.setStyle("-fx-border-color:red;");//給分頁組件添加 css樣式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.setTitle("場景上添加標題"); scene.getStylesheets().add("/test8.css");stage.show();//舞臺展現} }除了前面所應用的樣式之外,你還可以在程序中考慮使用如下的樣式來改變Pagination控件的外觀:
· -fx-max-page-indicator-count ——設置Page Indicator的最大數值。
· -fx-arrows-visible ——控制后一頁和前一頁箭頭按鈕的可見性,默認為true。
· -fx-tooltip-visible ——控制Page Indicator提示信息框的可見性,默認為true。
· -fx-page-information-visible ——控制頁面信息的可見性,默認為true。
· -fx-page-information-alignment ——設置頁面信息的對齊方式
總結
以上是生活随笔為你收集整理的javaFx学习之分页控件Pagination的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给自己的软件制作注册码
- 下一篇: java创建access数据库_使用Ja