不显示浮动 TabPane 的样式

Style for floating TabPane does not appear

尝试通过添加 CSS 文件来自定义浮动 TabPane,如下例所示。新样式不会仅针对浮动选项卡窗格显示 - 对于常规窗格,样式按预期显示。

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

  @Override
  public void start(Stage stage)
  {
    Parent root = createContentPane();
    root.getStylesheets().add(getClass().getResource("/floatingTabPane.css").toExternalForm());        
    Scene scene = new Scene(root, 1000, 800);    
    stage.setScene(scene);

    stage.setTitle(getClass().getSimpleName());
    stage.show();
  }

  private Parent createContentPane()
  {
    TabPane tabPane = new TabPane();
    tabPane.getStyleClass().add(TabPane.STYLE_CLASS_FLOATING);

    addTab(tabPane, "Tab 1", new StackPane());
    addTab(tabPane, "Tab 2", new StackPane());
    addTab(tabPane, "Tab 3", new StackPane());
    tabPane.getSelectionModel().selectLast();

    return new BorderPane(tabPane);
  }

  private void addTab(TabPane tabPane, String name, Node content)
  {
    Tab tab = new Tab();
    tab.setText(name);
    tab.setContent(content);
    tabPane.getTabs().add(tab);    
  }  
}

FloatingTabPane.css:

    .tab-pane.floating > .tab-header-area > .tab-header-background {
    -fx-border-color: red;
    -fx-border-width: 2;
    -fx-background-color: cyan;
}

这很有趣。问题是因为 headerBackground 可见性设置为 false,如果您使用 FLOATING 样式 class。

如果您在 TabPaneSkin 中搜索,您会发现:

if (isFloatingStyleClass()) {
     headerBackground.setVisible(false); // <---- Imp part
} else {
     headerBackground.resize(snapSize(getWidth()), snapSize(getHeight()));
     headerBackground.setVisible(true);
}

由于其可见性设置为 false,您最好的办法是在 tab-header-area 而不是 tab-header-background

上进行更改
.tab-pane.floating > .tab-header-area {
    -fx-border-color: red;
    -fx-border-width: 2;
    -fx-background-color: cyan;
}

这会在选项卡上留下一条细红线,但这总比没有样式要好 ;)