javafx_mytabpane_第1頁
javafx_mytabpane_第2頁
javafx_mytabpane_第3頁
javafx_mytabpane_第4頁
javafx_mytabpane_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、yunhaifeiwu1 JavaFx自之義控件之tabpane官方自有的tabpane不錯,但是TabPane的頭的右邊,設(shè)置一下功能按鈕,就很難實現(xiàn)了;又是為了展現(xiàn)如保用官方的GUI組件,實現(xiàn)自義控件,故有該控件設(shè)計。 這控件僅僅是簡單粗暴的測試,對于想要用于生產(chǎn)要謹慎。PS:tab的關(guān)閉圖標并沒有實現(xiàn)。實現(xiàn)后的測試界面如下:1.1 思路利用官方的ToggleGroup組件,該組件完成的功能是 可以在一組按鈕中進行選擇,可以減化難度。同時提供一組存儲tab的數(shù)組, 再提供一個stackPane,展現(xiàn)當前選中的tab. 當按下一個按鈕時,自動把該按鈕對應(yīng)的tab 送入stackPane中展現(xiàn)

2、出來,從而實現(xiàn)tabpane基本功能。本組件實現(xiàn)中,也展示了 javaFX 的CSS使用。1.2 FtabPane.java所寫的TabPane 僅有一個類。具體源代碼如下: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)異常,請開發(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有頭標題 * param ic

19、on tab頭的圖標 * 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 測試文件測試文件運行的效果圖如下:源代碼如下: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等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論