JavaFX Accordion控件

时间:2020-01-09 10:36:33  来源:igfitidea点击:

JavaFX Accordion控件是一个容器控件,它可以在内部包含几个部分,每个部分的内容都可以展开或者折叠。 Accordion控件由JavaFX类javafx.scene.control.Accordion实现。其中显示的部分由JavaFX TitledPane控件组成。

JavaFX手风琴示例

这是一个完整的JavaFXAccordion示例,因此我们可以快速了解其用法:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class AccordionExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {

        Accordion accordion = new Accordion();

        TitledPane pane1 = new TitledPane("Boats" , new Label("Show all boats available"));
        TitledPane pane2 = new TitledPane("Cars"  , new Label("Show all cars available"));
        TitledPane pane3 = new TitledPane("Planes", new Label("Show all planes available"));

        accordion.getPanes().add(pane1);
        accordion.getPanes().add(pane2);
        accordion.getPanes().add(pane3);

        VBox vBox = new VBox(accordion);
        Scene scene = new Scene(vBox);

        primaryStage.setScene(scene);

        primaryStage.show();
    }
}

创建手风琴

在使用JavaFXAccordion控件之前,必须首先实例化它。我们只需使用Javanew命令实例化它,如下所示:

Accordion accordion = new Accordion();

将TitledPane对象添加到手风琴

JavaFX" Accordion"内部显示的每个部分均由JavaFX TitledPane表示。要将部分添加到" Accordion"控件中,请在每个部分的" TitledPane"上创建,然后将其添加到" Accordion"中。这是在JavaFXAccordion中添加TitledPane部分的示例:

Accordion accordion = new Accordion();

TitledPane pane1 = new TitledPane("Boats" , new Label("Show all boats available"));
TitledPane pane2 = new TitledPane("Cars"  , new Label("Show all cars available"));
TitledPane pane3 = new TitledPane("Planes", new Label("Show all planes available"));

accordion.getPanes().add(pane1);
accordion.getPanes().add(pane2);
accordion.getPanes().add(pane3);

将手风琴添加到场景

要使JavaFXAccordion可见,必须将其添加到场景图中。这是一个向JavaFX场景图添加JavaFXAccordion的示例:

Accordion accordion = new Accordion();

VBox vBox = new VBox(accordion);
Scene scene = new Scene(vBox);

primaryStage.setScene(scene);

primaryStage.show();