JavaFX - 垂直手风琴

Iva*_*son 3 javafx accordion custom-component

您好,我有一个关于 JavaFX 的问题。我担心答案是剥皮,我对此一无所知,但在这里。

我想在 JavaFX 中制作 Accordion/TabPane 交叉。我将尝试用文字解释自己,但更进一步,我已经包含了我正在尝试制作的图像。

所以,我想制作一个左侧有一个按钮的 JavaFX 容器,当点击该按钮时,将在这个初始容器上移动一个不同的相关容器。再次按下时,顶部容器将移回左侧。我希望按钮和顶部容器从左到右一起移动,然后再向后移动,就好像它们相互连接一样。

需要明确的是,两个不同的容器最好以平滑的方式在手风琴的情况下进行转换。

在此处输入图片说明

fab*_*ian 6

将“标准”内容和滑块的内容(aHBox包含一个按钮和滑块内容)放在 a 中,StackPane并使用动画设置translateX使滑块移入和移出视图的方式:

@Override
public void start(Stage primaryStage) {
    Button someButton = new Button("Sample content");

    StackPane stackPane = new StackPane(someButton);
    stackPane.setPrefSize(500, 500);
    stackPane.setStyle("-fx-background-color: blue;");

    Region sliderContent = new Region();
    sliderContent.setPrefWidth(200);
    sliderContent.setStyle("-fx-background-color: red; -fx-border-color: orange; -fx-border-width: 5;");

    Button expandButton = new Button(">");

    HBox slider = new HBox(sliderContent, expandButton);
    slider.setAlignment(Pos.CENTER);
    slider.setPrefWidth(Region.USE_COMPUTED_SIZE);
    slider.setMaxWidth(Region.USE_PREF_SIZE);

    // start out of view
    slider.setTranslateX(-sliderContent.getPrefWidth());
    StackPane.setAlignment(slider, Pos.CENTER_LEFT);

    // animation for moving the slider
    Timeline timeline = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(slider.translateXProperty(), -sliderContent.getPrefWidth())),
            new KeyFrame(Duration.millis(500), new KeyValue(slider.translateXProperty(), 0d))
    );

    expandButton.setOnAction(evt -> {
        // adjust the direction of play and start playing, if not already done
        String text = expandButton.getText();
        boolean playing = timeline.getStatus() == Animation.Status.RUNNING;
        if (">".equals(text)) {
            timeline.setRate(1);
            if (!playing) {
                timeline.playFromStart();
            }
            expandButton.setText("<");
        } else {
            timeline.setRate(-1);
            if (!playing) {
                timeline.playFrom("end");
            }
            expandButton.setText(">");
        }
    });

    stackPane.getChildren().add(slider);

    final Scene scene = new Scene(stackPane);

    primaryStage.setScene(scene);
    primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)