带有多个打开窗格的 JavaFX 手风琴 [英] JavaFX accordion with multiple open panes

查看:24
本文介绍了带有多个打开窗格的 JavaFX 手风琴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JavaFX 中是否可以有一个带有 1 个以上打开窗格的手风琴?

Is it possible to have an accordion with more then 1 open pane in JavaFX?

推荐答案

不,JavaFX 2.2 手风琴 一次只能打开一个窗格.

No, a JavaFX 2.2 Accordion can only have one open pane at a time.

我为允许您一次在手风琴中打开多个窗格的功能,但目前尚未实现该功能请求.

I created an enhancement request (JDK-8090554 StackedTitledPanes control) for a feature which allows you to open more than one pane in the accordion at a time, however the feature request has currently not been implemented.

与此同时,您可以通过创建多个 TitledPane 实例并将它们放置在 VBox.

In the meantime, you can construct a similar control yourself quite easily by creating multiple TitledPane instances and placing these in a VBox.

private VBox createStackedTitledPanes() {
  final VBox stackedTitledPanes = new VBox();
  stackedTitledPanes.getChildren().setAll(
    new TitledPane("Pane 1",  contentNode1),
    new TitledPane("Pane 2",  contentNode2),
    new TitledPane("Pane 3",  contentNode3)
  );
  ((TitledPane) stackedTitledPanes.getChildren().get(0)).setExpanded(true);

  return stackedTitledPanes;
}

如有必要,您可以将包含窗格的 VBox 包装在 ScrollPane,以便所有展开的窗格的内容在它们的区域溢出可用区域时可用.

If necessary, you can wrap the VBox containing your panes in a ScrollPane, so that the contents of all of your expanded panes can be usable if their area overflows the available area.

我创建了一个示例解决方案(图标是来自:http://www.fasticon.com).

I created a sample solution (icons are linkware from: http://www.fasticon.com).

更新

针对可滚动的 TitledPanes 堆栈对先前外部链接的示例解决方案进行了现代化和内联处理.

Modernized and inlined the previously externally linked example solution for a scrollable stack of TitledPanes.

另外,请注意,在现代 JavaFX 环境中,默认样式略有不同(在 TitledPane 内容背景等内容中,默认情况下渐变较少),因此它看起来与此答案中的先前图像略有不同,但除此之外行为类似.

Also, note that in a modern JavaFX environment the default styling is a bit different (fewer gradients in by default in things like the TitledPane content background), so it will look slightly different than the prior image in this answer, but otherwise behavior is similar.

import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class StackedPanes extends Application {
    // image license: linkware - backlink to http://www.fasticon.com
    private static final Image BLUE_FISH = new Image("http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Blue-Fish-icon.png");
    private static final Image RED_FISH = new Image("http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Red-Fish-icon.png");
    private static final Image YELLOW_FISH = new Image("http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Yellow-Fish-icon.png");
    private static final Image GREEN_FISH = new Image("http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Green-Fish-icon.png");

    @Override
    public void start(Stage stage) {
        VBox stackedTitledPanes = createStackedTitledPanes();

        ScrollPane scroll = new ScrollPane(stackedTitledPanes);
        scroll.setFitToWidth(true);
        scroll.setFitToHeight(true);
        scroll.setPrefWidth(410);
        scroll.setStyle("-fx-base: cadetblue;");

        stage.setTitle("Fishy, fishy");
        Scene scene = new Scene(scroll);
        stage.setScene(scene);
        stage.show();
    }

    private VBox createStackedTitledPanes() {
        final VBox stackedTitledPanes = new VBox();
        stackedTitledPanes.getChildren().setAll(
                createTitledPane("One Fish", GREEN_FISH),
                createTitledPane("Two Fish", YELLOW_FISH, GREEN_FISH),
                createTitledPane("Red Fish", RED_FISH),
                createTitledPane("Blue Fish", BLUE_FISH)
        );
        ((TitledPane) stackedTitledPanes.getChildren().get(0)).setExpanded(true);

        return stackedTitledPanes;
    }

    public TitledPane createTitledPane(String title, Image... images) {
        FlowPane content = new FlowPane();
        for (Image image : images) {
            ImageView imageView = new ImageView(image);
            content.getChildren().add(imageView);

            FlowPane.setMargin(imageView, new Insets(10));
        }
        content.setAlignment(Pos.TOP_CENTER);

        TitledPane pane = new TitledPane(title, content);
        pane.setExpanded(false);

        return pane;
    }

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

这篇关于带有多个打开窗格的 JavaFX 手风琴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆