如何在 JavaFX 中建立手風琴?


標題面板只是一個帶有標題的面板。它包含一個或多個使用者介面元素,如按鈕、標籤等,你可以展開和摺疊它。手風琴是一個包含一個或多個標題頁面的容器,你一次只能開啟一個標題面板。

可以透過例項化 javafx.scene.control.Accordion 類在 JavaFX 中建立 手風琴getPanes() 方法返回在當前手風琴中包含所有面板的列表。

向手風琴中新增標題面板 -

  • 建立所需數量的標題面板。

  • 為建立的面板設定內容。

  • 使用 getPanes() 方法檢索當前手風琴的可觀察列表。

  • 使用 addAll() 方法將所有建立的面板新增到列表中。

示例

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TitledPane;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class AccordionExample extends Application {
   @Override
   public void start(Stage stage) {
      //Creating toggle buttons
      RadioButton button1 = new RadioButton("Apache Tika");
      RadioButton button2 = new RadioButton("JavaFX");
      RadioButton button3 = new RadioButton("Java ML");
      ToggleGroup group1 = new ToggleGroup();
      group1.getToggles().addAll(button1, button2, button3);
      //Adding the toggle button to the pane
      VBox box1 = new VBox(10);
      box1.setPadding(new Insets(10));
      box1.getChildren().addAll(button1, button2, button3);
      //Creating the TitlePane
      TitledPane pane1 = new TitledPane("Java Libraries", box1);
      pane1.setLayoutX(1);
      pane1.setLayoutY(1);
      //Creating toggle buttons
      RadioButton button4 = new RadioButton("HBase");
      RadioButton button5 = new RadioButton("MongoDB");
      RadioButton button6 = new RadioButton("Neo4j");
      ToggleGroup group2 = new ToggleGroup();
      group2.getToggles().addAll(button4, button5, button6);
      //Adding the toggle button to the pane
      VBox box = new VBox(10);
      box.setPadding(new Insets(10));
      box.getChildren().addAll(button4, button5, button6);
      //Creating the TitlePane
      TitledPane pane2 = new TitledPane("NoSQL Databases", box);
      pane2.setLayoutX(1);
      pane2.setLayoutY(1);
      //Creating a Accordion
      Accordion accor = new Accordion();
      accor.getPanes().add(pane1);
      accor.getPanes().add(pane2);
      VBox vbox = new VBox(accor);
      //Setting the stage
      Scene scene = new Scene(vbox, 595, 150, Color.BEIGE);
      stage.setTitle("Accordion Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於: 16-5-2020

772 次瀏覽

開啟你的職業道路

完成課程獲得認證

開始學習
廣告
© . All rights reserved.