2017-02-09 74 views

回答

0

您可以简单地设置max heightTabPane的:

public class Main extends Application { 

    private static final int TABPANE_HEADER_HEIGHT = 29; 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     BorderPane root = new BorderPane(); 

     // Add simple tabs 
     TabPane tp = new TabPane(); 
     tp.getTabs().add(new Tab("Tab1", new Label(" Content of the first tab"))); 
     tp.getTabs().add(new Tab("Tab2", new Label(" Content of the second tab"))); 

     // Create the Tab which hides the content 
     Tab hideTab = new Tab("Hide", new Label(" Content of the third tab")); 
     tp.getTabs().add(hideTab); 

     hideTab.selectedProperty().addListener((obs, oldval, newval) -> 
      tp.setMaxHeight(((newval) ? TABPANE_HEADER_HEIGHT : -1))); 

     root.setTop(tp); 

     Scene scene = new Scene(root, 300, 275); 
     scene.getStylesheets().addAll(getClass().getResource("style.css").toExternalForm()); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

结果:

enter image description here


注意

您可以通过添加一个名为例如.tab-pane的新伪类来使用CSS做同样的事情。 tabcontenthidden。在这个伪类中,TabPane的最大高度是标签的高度。

的style.css

.root { TAB_HEADER_HEIGHT: 29; } 

.tab-pane:tabcontenthidden { -fx-max-height: TAB_HEADER_HEIGHT; } 

.tab-pane { 
    -fx-max-height: -1; 
    -fx-background-color: orange; 
} 

在Java代码中,你可以创建一个PseudoClass

PseudoClass TABPANE_CONTENT_HIDDEN = PseudoClass.getPseudoClass("tabcontenthidden"); 

,你可以激活这个伪类与pseudoClassStateChanged方法:

tabPane.pseudoClassStateChanged(TABPANE_CONTENT_HIDDEN, true); // false to show 

注2

您可以在此answer(一个键隐藏和显示)添加Button s到片区域就像这可能不是一个额外Tab更符合人体工程学。

相关问题