當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaFX 類 TitledPane用法及代碼示例

TitledPane類是JavaFX的一部分。 TitledPane類創建一個麵板,該麵板的標題可以打開或關閉。 TitledPane類擴展了Labeled類。

該類的構造函數:

  • TitledPane():創建一個新的TitledPane對象。
  • TitledPane(String t, Node n):使用指定的內容和標題創建一個新的TitledPane對象。

常用方法:


方法 說明
getContent() 返回TitledPane的內容。
isAnimated() 返回TitledPane是否動畫。
isCollapsible() 返回TitledPane是否可折疊。
isExpanded() 返回TitledPane是否展開。
setAnimated(boolean v) 設置TiledPane的動畫狀態。
setCollapsible(布爾v 設置TiledPane的可折疊狀態。
setContent(Node v) 設置標題窗格的內容窗格。
setExpanded(boolean v) 設置TitledPane的展開狀態。

以下示例程序旨在說明TitlePane類的用法:

  1. Java程序創建TitledPane並為其添加標簽:
    • 在此程序中,我們將創建一個TitledPane並為其添加標簽。
    • Label將包含使用fileInputStream導入的圖片。
    • 將此圖片添加到標簽。
    • 將標簽添加到titled_pane。
    • 現在將titled_pane添加到場景並將場景添加到舞台。
    • 調用show()函數以顯示最終結果。
    // Java program to create a TitledPane 
    // and add a label to it. 
    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.stage.Stage; 
    import javafx.scene.layout.*; 
    import javafx.scene.paint.*; 
    import javafx.scene.text.*; 
    import javafx.geometry.*; 
    import javafx.scene.layout.*; 
    import javafx.scene.shape.*; 
    import javafx.scene.paint.*; 
    import javafx.scene.*; 
    import java.io.*; 
    import javafx.scene.image.*; 
       
    public class TitledPane_1 extends Application { 
       
        // launch the application 
        public void start(Stage stage) 
        { 
       
            try { 
       
                // set title for the stage 
                stage.setTitle("Titled Pane"); 
       
                // create a input stream 
                FileInputStream input = new FileInputStream("D:\\GFG.png"); 
       
                // create a image 
                Image image = new Image(input); 
       
                // create a image View 
                ImageView imageview = new ImageView(image); 
       
                // create Label 
                Label label = new Label("", imageview); 
       
                // create TiledPane 
                TitledPane titled_pane = new TitledPane("Titled Pane", label); 
       
                // create a scene 
                Scene scene = new Scene(titled_pane, 500, 300); 
       
                // set the scene 
                stage.setScene(scene); 
       
                stage.show(); 
            } 
       
            catch (Exception e) { 
       
                System.out.println(e.getMessage()); 
            } 
        } 
       
        // Main Method 
        public static void main(String args[]) 
        { 
       
            // launch the application 
            launch(args); 
        } 
    }

    輸出:

  2. Java程序,用於創建TitledPane,聲明其是否為動畫,是否可折疊並為其添加標簽:
    • 在此程序中,我們將創建一個TitledPane並為其添加標簽。
    • Label將包含使用fileInputStream導入的圖片。
    • 將此圖片添加到標簽,並將標簽添加到titled_pane。
    • 將titled_pane添加到場景並將場景添加到舞台。
    • 調用show()函數以顯示最終結果。
    • 使用setAnimated()函數將動畫設置為false,使用setCollapsable()函數將可折疊動畫設置為false。
    // Java program to create a TitledPane, state  
    // whether it is animated or not, collapsible 
    // or not and add a label to it 
    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.stage.Stage; 
    import javafx.scene.layout.*; 
    import javafx.scene.paint.*; 
    import javafx.scene.text.*; 
    import javafx.geometry.*; 
    import javafx.scene.layout.*; 
    import javafx.scene.shape.*; 
    import javafx.scene.paint.*; 
    import javafx.scene.*; 
    import java.io.*; 
    import javafx.scene.image.*; 
      
    public class TitledPane_2 extends Application { 
      
        // launch the application 
        public void start(Stage stage) 
        { 
      
            try { 
      
                // set title for the stage 
                stage.setTitle("Titled Pane"); 
      
                // create a input stream 
                FileInputStream input = new FileInputStream("D:\\GFG.png"); 
      
                // create a image 
                Image image = new Image(input); 
      
                // create a image View 
                ImageView imageview = new ImageView(image); 
      
                // create Label 
                Label label = new Label("", imageview); 
      
                // create TiledPane 
                TitledPane titled_pane = new TitledPane("Titled Pane", label); 
      
                // set Animated 
                titled_pane.setAnimated(false); 
      
                // set collapsible 
                titled_pane.setCollapsible(false); 
      
                // create a scene 
                Scene scene = new Scene(titled_pane, 500, 300); 
      
                // set the scene 
                stage.setScene(scene); 
      
                stage.show(); 
            } 
      
            catch (Exception e) { 
      
                System.out.println(e.getMessage()); 
            } 
        } 
      
        // Main Method 
        public static void main(String args[]) 
        { 
      
            // launch the application 
            launch(args); 
        } 
    }

    輸出:

注意:以上程序可能無法在在線IDE中運行,請使用離線編譯器。

參考:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TitledPane.html



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | TitledPane Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。