GroupBox / TitledBorder in JavaFX 2?

后端 未结 6 784
小鲜肉
小鲜肉 2020-11-28 05:57

Is there something like a GroupBox or TitledBorder available on JavaFX 2?

Thanks for any hint :-)

6条回答
  •  被撕碎了的回忆
    2020-11-28 06:43

    No such standard control, but it it is easy to create your own. Here is a sample implementation:

    /** Places content in a bordered pane with a title. */
    class BorderedTitledPane extends StackPane {
      BorderedTitledPane(String titleString, Node content) {
        Label title = new Label(" " + titleString + " ");
        title.getStyleClass().add("bordered-titled-title");
        StackPane.setAlignment(title, Pos.TOP_CENTER);
    
        StackPane contentPane = new StackPane();
        content.getStyleClass().add("bordered-titled-content");
        contentPane.getChildren().add(content);
    
        getStyleClass().add("bordered-titled-border");
        getChildren().addAll(title, contentPane);
      }
    }
    

    And the accompanying css for it:

    .label {
      -fx-font: 28px Vivaldi;
    }
    
    .bordered-titled-title {
      -fx-background-color: white;
      -fx-translate-y: -16;
    }
    
    .bordered-titled-border {
      -fx-content-display: top;
      -fx-border-insets: 20 15 15 15;
      -fx-background-color: white;
      -fx-border-color: black;
      -fx-border-width: 2;
    }
    
    .bordered-titled-content {
      -fx-padding: 26 10 10 10;
    }
    

    The code is from a example I created in response to an Oracle JavaFX forum thread post "Equivalent to BorderFactory.createTitledBorder".

    The output of the example program is as shown below.

    gettysburg

提交回复
热议问题