4

我想知道 JavaFX 是否包含使手风琴或标题窗格水平的方法。我找不到任何东西,但我想我应该问。本质上,最终目标是拥有一个可以展开以显示树视图的侧边栏。以下是我的意图图片:

折叠

展开

4

4 回答 4

2

JavaFX 2.2 中没有标准的水平方向 TitledPane。

您可以在JavaFX 问题跟踪器中创建一个功能请求。

实现您自己的水平 TitledPane 非常简单。

这是一个类似的演示,只是在标准窗格上使用动画。

Sai 的博客文章进一步解释了所涉及的技术:Sliding in JavaFX (It's all about clipping)

侧边栏可见 侧边栏隐藏

/** Animates a node on and off screen to the left. */
class SideBar extends VBox {
  /** @return a control button to hide and show the sidebar */
  public Button getControlButton() { return controlButton; }
  private final Button controlButton;

  /** creates a sidebar containing a vertical alignment of the given nodes */
  SideBar(final double expandedWidth, Node... nodes) {
    getStyleClass().add("sidebar");
    this.setPrefWidth(expandedWidth);

    // create a bar to hide and show.
    setAlignment(Pos.CENTER);
    getChildren().addAll(nodes);

    // create a button to hide and show the sidebar.
    controlButton = new Button("Collapse");
    controlButton.getStyleClass().add("hide-left");

    // apply the animations when the button is pressed.
    controlButton.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent actionEvent) {
        // create an animation to hide sidebar.
        final Animation hideSidebar = new Transition() {
          { setCycleDuration(Duration.millis(250)); }
          protected void interpolate(double frac) {
            final double curWidth = expandedWidth * (1.0 - frac);
            setPrefWidth(curWidth);
            setTranslateX(-expandedWidth + curWidth);
          }
        };
        hideSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent actionEvent) {
            setVisible(false);
            controlButton.setText("Show");
            controlButton.getStyleClass().remove("hide-left");
            controlButton.getStyleClass().add("show-right");
          }
        });

        // create an animation to show a sidebar.
        final Animation showSidebar = new Transition() {
          { setCycleDuration(Duration.millis(250)); }
          protected void interpolate(double frac) {
            final double curWidth = expandedWidth * frac;
            setPrefWidth(curWidth);
            setTranslateX(-expandedWidth + curWidth);
          }
        };
        showSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent actionEvent) {
            controlButton.setText("Collapse");
            controlButton.getStyleClass().add("hide-left");
            controlButton.getStyleClass().remove("show-right");
          }
        });

        if (showSidebar.statusProperty().get() == Animation.Status.STOPPED && hideSidebar.statusProperty().get() == Animation.Status.STOPPED) {
          if (isVisible()) {
            hideSidebar.play();
          } else {
            setVisible(true);
            showSidebar.play();
          }
        }
      }
    });
  }
}
于 2013-06-25T05:05:21.990 回答
1

你去:

@Override
public void start(Stage stage) throws Exception {       

    Label label1 = new Label("label 1");
    label1.setRotate(90);

    TitledPane pane1 = new TitledPane("titled pane 1", label1);
    pane1.setAlignment(Pos.CENTER);

    Label label2 = new Label("label 2");
    label2.setRotate(90);

    TitledPane pane2 = new TitledPane("titled pane 2", label2);
    pane2.setAlignment(Pos.CENTER);

    Accordion accordion = new Accordion();
    accordion.setRotate(270);
    accordion.getPanes().add(pane1);
    accordion.getPanes().add(pane2);

    HBox mainPane = new HBox(accordion);
    accordion.prefWidthProperty().bind(mainPane.heightProperty());
    accordion.prefHeightProperty().bind(mainPane.widthProperty());

    stage.setTitle("Horizontal Accordion");
    stage.setScene(new Scene(mainPane, 800, 600));
    stage.show();
}
于 2014-10-13T08:57:13.770 回答
-1

只需将以下行添加到手风琴并完成。

 accordion.setRotate(270);
于 2013-11-12T06:05:22.177 回答
-1

也许 JavaFx 不提供水平 TitledPane,但您可以将 TitledPane 旋转到 90 度,然后将要在其内容中设置的节点旋转到 270 度,然后就完成了。

这是给您的代码示例。

TitledPane titledPane = new TitledPane();

BorderPane borderPane = new BorderPane();
borderPane.setCenter(new Label("My Label")); //Or your tree view

borderPane.setRotate(270);

titledPane .setContent(borderPane);
于 2013-06-25T04:51:29.000 回答