




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、yunhaifeiwu1 JavaFx自之義控件之tabpane官方自有的tabpane不錯(cuò),但是TabPane的頭的右邊,設(shè)置一下功能按鈕,就很難實(shí)現(xiàn)了;又是為了展現(xiàn)如保用官方的GUI組件,實(shí)現(xiàn)自義控件,故有該控件設(shè)計(jì)。 這控件僅僅是簡(jiǎn)單粗暴的測(cè)試,對(duì)于想要用于生產(chǎn)要謹(jǐn)慎。PS:tab的關(guān)閉圖標(biāo)并沒有實(shí)現(xiàn)。實(shí)現(xiàn)后的測(cè)試界面如下:1.1 思路利用官方的ToggleGroup組件,該組件完成的功能是 可以在一組按鈕中進(jìn)行選擇,可以減化難度。同時(shí)提供一組存儲(chǔ)tab的數(shù)組, 再提供一個(gè)stackPane,展現(xiàn)當(dāng)前選中的tab. 當(dāng)按下一個(gè)按鈕時(shí),自動(dòng)把該按鈕對(duì)應(yīng)的tab 送入stackPane中展現(xiàn)
2、出來,從而實(shí)現(xiàn)tabpane基本功能。本組件實(shí)現(xiàn)中,也展示了 javaFX 的CSS使用。1.2 FtabPane.java所寫的TabPane 僅有一個(gè)類。具體源代碼如下:package basetech.better;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.logging.Logger;import perty.SimpleIntegerProperty;import java
3、fx.beans.value.ObservableValue;import javafx.geometry.HPos;import javafx.geometry.Pos;import javafx.scene.Node;import javafx.scene.control.Button;import javafx.scene.control.ScrollPane;import javafx.scene.control.Toggle;import javafx.scene.control.ToggleButton;import javafx.scene.control.ToggleGroup
4、;import javafx.scene.layout.AnchorPane;import javafx.scene.layout.BorderPane;import javafx.scene.layout.ColumnConstraints;import javafx.scene.layout.GridPane;import javafx.scene.layout.HBox;import javafx.scene.layout.StackPane;import javafx.scene.shape.Polygon;/* * * author yunhaifeiwu */public clas
5、s FTabPane extends BorderPane private static final Logger LOG = Logger.getLogger(FTabPane.class.getName(); private final String USER_DATA="fyh_userData" private final ToggleGroup group = new ToggleGroup(); private final List<Node> tabs=new ArrayList(); private final List<ToggleBut
6、ton> buttons=new ArrayList(); private final HBox lbox=new HBox(); private final HBox rbox=new HBox(); private final GridPane top=new GridPane(); private StackPane centerPane; private Node current; private int currentIndex; public SimpleIntegerProperty currentIndexProperty=new SimpleIntegerPropert
7、y() ; ScrollPane sp=new ScrollPane(); ColumnConstraints column1; ColumnConstraints column2; public FTabPane() super(); top.getStyleClass().add("ftabpane-toolbar"); rbox.setAlignment(Pos.CENTER_RIGHT); Button bt=new Button(); bt.heightProperty().addListener(obj,old,new1)-> int h=(Double)
8、new1).intValue(); if (h<=0 | bt.getGraphic()!=null)return; h=h/2; Polygon polygon1 = getLeftTrigon(h); bt.setGraphic(polygon1); ); bt.getStyleClass().add("ftabpane-button"); bt.setOnAction(e-> if (current=null) return; if (currentIndex>0) setSelectedTab(currentIndex-1); ); Button
9、bt1=new Button(); bt1.heightProperty().addListener(obj,old,new1)-> int h=(Double)new1).intValue(); if (h<=0 | bt1.getGraphic()!=null)return; h=h/2; Polygon polygon1 = getRightTrigon(h); bt1.setGraphic(polygon1); ); bt1.getStyleClass().add("ftabpane-button"); bt1.setOnAction(e-> if
10、 (current=null) return; if (currentIndex>-1 && currentIndex<buttons.size()-1) setSelectedTab(currentIndex+1); ); rbox.setSpacing(2); rbox.getChildren().addAll(bt,bt1); sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); sp.setFitToHeight(true); sp.setContent(lbox); sp.getStyleClass().c
11、lear(); sp.getStyleClass().add("ftabpane-toolbar"); lbox.setAlignment(Pos.BOTTOM_LEFT); lbox.setSpacing(2); top.add(sp, 0, 0); AnchorPane rboxa=new AnchorPane(); rboxa.getChildren().add(rbox); AnchorPane.setRightAnchor(rbox, 0.0); AnchorPane.setBottomAnchor(rbox, 0.0); top.add(rboxa, 1, 0)
12、; column1 = new ColumnConstraints(); column1.setHalignment(HPos.RIGHT); column2 = new ColumnConstraints(); column2.setHalignment(HPos.RIGHT); top.getColumnConstraints().addAll(column1, column2); top.widthProperty().addListener(obj,old,new1)-> Double w=(Double) rboxa.getWidth(); w=(w=0)?(100.0):w;
13、 column1.setPrefWidth(top.getWidth()-w); column2.setPrefWidth(w); ); setTop(top); centerPane=new StackPane(); centerPane.getStyleClass().clear(); centerPane.getStyleClass().add("ftabpane-center"); this.setCenter(centerPane); group.selectedToggleProperty().addListener(ObservableValue<? e
14、xtends Toggle> observable, Toggle oldValue, Toggle select) -> if (select = null) return; Node node=(Node) (Map<String,Object>)select.getUserData().get(USER_DATA); if (node=null) LOG.warning("所選擇的tab 出現(xiàn)異常,請(qǐng)開發(fā)人員檢查其userData設(shè)定值"); return; int pos=buttons.indexOf(select)+1; currentI
15、ndexProperty.set(pos-1); sp.setHvalue(Double.valueOf(pos)/Double.valueOf(buttons.size(); Node tab=tabs.get(pos-1); current =tab; currentIndex=pos-1; centerPane.getChildren().clear(); centerPane.getChildren().add(tab); ); public Node getSelectedTab() return current; public static Polygon getLeftTrigo
16、n(int h) Polygon polygon1 = new Polygon(new double 0, h/2, h*0.6, 0, h*0.6, h ); polygon1.getStyleClass().add("left-trigon"); return polygon1; public static Polygon getRightTrigon(int h) Polygon polygon1 = new Polygon(new double 0, 0, 0, h, h*0.6,h/2 ); polygon1.getStyleClass().add("r
17、ight-trigon"); return polygon1; public void setSelectedTab(int index) if (index<0 | index>=buttons.size() return; ToggleButton bt=buttons.get(index); if (bt=null) return; group.selectToggle(bt); current=tabs.get(index); currentIndex=index; currentIndexProperty.set(index); sp.setHvalue(Dou
18、ble.valueOf(index)/Double.valueOf(buttons.size(); Node tab=tabs.get(index); centerPane.getChildren().clear(); centerPane.getChildren().add(current); public void setSelectedTab(Node node) if (node=null) return; int index=tabs.indexOf(node); setSelectedTab(index); /* * * param title tab有頭標(biāo)題 * param ic
19、on tab頭的圖標(biāo) * param nodeTab tab內(nèi)容 */ public void addTab(String title,Node icon ,Node nodeTab) if (nodeTab=null) return; tabs.add(nodeTab); Map<String,Object> map=new HashMap(); map.put(USER_DATA, nodeTab); nodeTab.getStyleClass().add("ftabpane-font"); ToggleButton bt=title=null?new To
20、ggleButton():new ToggleButton(title); bt.setGraphic(icon); bt.setUserData(map); bt.setToggleGroup(group); bt.getStyleClass().clear(); bt.getStyleClass().add("ftabpane-button"); lbox.getChildren().add(bt); buttons.add(bt); setSelectedTab(nodeTab); public void addTab( Node icon ,Node nodeTab
21、) addTab("",icon,nodeTab); public void addTab( String title ,Node nodeTab) addTab(title,null,nodeTab); public void addTab( Node nodeTab) addTab("",null,nodeTab); public void removeTab(Node node ) if(node=null) return; int index=tabs.indexOf(node); ToggleButton bt=buttons.get(inde
22、x); int size=buttons.size(); group.getToggles().remove(bt); buttons.remove(bt); tabs.remove(node); lbox.getChildren().remove(bt); if(index>0) index=index-1; else if (index<0 )&& size>0 ) index=index+1; if(buttons.size()=0) centerPane.getChildren().clear(); group.getToggles().clear()
23、; else if(index>=0 && index<buttons.size() bt=buttons.get(index); node=tabs.get(index); setSelectedTab( node); public Node getCurrent() return current; public int getCurrentIndex() return currentIndex; public ToggleGroup getGroup() return group; public HBox getTopLeft() return lbox; pu
24、blic HBox getTopRight() return rbox; 1.3 FtabPaneTest.css這是TabPane所要使用到的css文件樣式。Ftabpane開頭的都是 tabPane所要用到的。完整文件如下:.root -fx-background-color: linear-gradient(#B0D1D8, #F1EAF5,#AACCD5);.ftabpane-center -fx-background-color: linear-gradient(#B0D1D8, #F1EAF5,#AACCD5); .ftabpane-font -fx-font-size: 46px
25、; -fx-text-fill: black; -fx-font-family: "Serif" -fx-font-weight: bold; .ftabpane-button -fx-background-color: linear-gradient(#9CC5CE,#75B1BD,#94C2CC); -fx-text-fill: black; -fx-font-size:13px; -fx-padding:5;.ftabpane-button:hover /* -fx-stroke: #F1EAF5;*/ -fx-background-color: #FEF7F1; -
26、fx-border-color: #75B1BD; -fx-border-insets: 1 1 1 1; -fx-border-style:solid; -fx-text-fill: #0F6143;.ftabpane-button:selected -fx-background-color: #FEF7F1; -fx-border-color: #75B1BD; /*-fx-border-insets: 1 1 1 1;*/ -fx-border-style:solid; -fx-text-fill: red;.ftabpane-toolbar -fx-background-color:
27、linear-gradient( #4D8F9D,#305B64,#569FAE); .left-trigon -fx-fill: red;.right-trigon -fx-fill: red;1.4 測(cè)試文件測(cè)試文件運(yùn)行的效果圖如下:源代碼如下:package basetech.better;import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.Label;import javafx.scen
28、e.layout.BorderPane;import javafx.scene.layout.HBox;import javafx.stage.Stage;public class FTabPaneTest1 extends Application int i=0; Override public void start(Stage primaryStage) throws Exception BorderPane bp=new BorderPane(); FTabPane tabpane=new FTabPane();/ tabpane.getTopRight().getChildren().clear();/ tabpane.getTo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 青少年控?zé)熃逃ぷ饔?jì)劃
- 七年級(jí)下冊(cè)班主任班級(jí)紀(jì)律維護(hù)計(jì)劃
- 鋼結(jié)構(gòu)冬季施工機(jī)械安拆措施
- 機(jī)電安裝工程資源投入計(jì)劃及保障措施
- 以小說為翼翱翔審美之空:高中語文閱讀教學(xué)新探
- 以多元智力理論賦能小學(xué)數(shù)學(xué)第一學(xué)段教學(xué):策略與實(shí)踐
- 森林防火災(zāi)害預(yù)防心得體會(huì)
- 甲亢危象搶救流程中的心理疏導(dǎo)他
- 護(hù)理質(zhì)量提升中的患者主動(dòng)參與流程
- 破產(chǎn)管理人審計(jì)配合計(jì)劃
- 2024中國醫(yī)藥行業(yè)人才發(fā)展報(bào)告-智聯(lián)招聘-202404
- 《安全生產(chǎn)課件-氧化鋁粉塵隱患與控制》
- 汽輪機(jī)檢修安全施工方案
- 2024年課外閱讀《中國古代寓言故事》知識(shí)考試題與答案
- DB32/T 4699-2024 企業(yè)應(yīng)急能力評(píng)估規(guī)范
- MATLAB運(yùn)用simulink建立簡(jiǎn)單的單機(jī)無窮大系統(tǒng)仿真模擬數(shù)字電子技術(shù)
- 2024屆貴州省貴陽市普通高中化學(xué)高二下期末學(xué)業(yè)水平測(cè)試模擬試題含解析
- 心理輔導(dǎo)室配置清單及預(yù)算
- GB/T 23101.3-2023外科植入物羥基磷灰石第3部分:結(jié)晶度和相純度的化學(xué)分析和表征
- 石英晶體諧振器培訓(xùn)資料
- 紫羅蘭永恒花園
評(píng)論
0/150
提交評(píng)論