可以使用手风琴(accordion)控件对标题窗格进行分组。

  1. import javafx.application.Application;
  2. import javafx.scene.Group;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.Accordion;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.TitledPane;
  7. import javafx.stage.Stage;
  8. public class Main extends Application {
  9. public static void main(String[] args) {
  10. Application.launch(args);
  11. }
  12. @Override
  13. public void start(Stage primaryStage) {
  14. Group g = new Group();
  15. Scene scene = new Scene(g, 550, 250);
  16. TitledPane t1 = new TitledPane("T1", new Button("B1"));
  17. TitledPane t2 = new TitledPane("T2", new Button("B2"));
  18. TitledPane t3 = new TitledPane("T3", new Button("B3"));
  19. Accordion accordion = new Accordion();
  20. accordion.getPanes().addAll(t1, t2, t3);
  21. g.getChildren().add(accordion);
  22. primaryStage.setScene(scene);
  23. primaryStage.show();
  24. }
  25. }

Video_2022-04-19_023125.wmv (135.69KB)

手风琴事件

  1. import javafx.application.Application;
  2. import javafx.beans.value.ObservableValue;
  3. import javafx.scene.Group;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Accordion;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.TitledPane;
  8. import javafx.stage.Stage;
  9. public class Main extends Application {
  10. public static void main(String[] args) {
  11. Application.launch(args);
  12. }
  13. @Override
  14. public void start(Stage primaryStage) {
  15. Group g = new Group();
  16. Scene scene = new Scene(g, 550, 250);
  17. TitledPane t1 = new TitledPane("T1", new Button("B1"));
  18. TitledPane t2 = new TitledPane("T2", new Button("B2"));
  19. TitledPane t3 = new TitledPane("T3", new Button("B3"));
  20. Accordion accordion = new Accordion();
  21. accordion.getPanes().addAll(t1, t2, t3);
  22. accordion.expandedPaneProperty()
  23. .addListener((ObservableValue<? extends TitledPane> ov, TitledPane old_val, TitledPane new_val) -> {
  24. if (new_val != null) {
  25. System.out.println(accordion.getExpandedPane().getText());
  26. }
  27. });
  28. g.getChildren().add(accordion);
  29. primaryStage.setScene(scene);
  30. primaryStage.show();
  31. }
  32. }

Video_2022-04-19_023700.wmv (347.67KB)