如您所见,当前关闭按钮位于顶部。
如何将它放在右侧?
public class TabbedPaneTest extends Application
{
@Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("TabPane Test");
TabPane tabPane = new TabPane();
tabPane.setStyle("-fx-tab-min-height: 100");
tabPane.setRotateGraphic(true);
tabPane.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);
tabPane.getTabs().addAll(tab("Tab 1"), tab("Tab 2"));
tabPane.setSide(Side.LEFT);
primaryStage.setScene(new Scene(tabPane, 400, 200));
primaryStage.show();
}
static Tab tab(String labelText)
{
Tab tab = new Tab();
Label label = new Label(labelText);
label.setRotate(90);
tab.setGraphic(new StackPane(label));
tab.setContent(new Label(" " + labelText + " content"));
return tab;
}
public static void main(String[] args)
{
launch(args);
}
}
最佳答案
这会有点 hacky。
基于CSS reference TabPane
和 TabPaneSkin
的源代码:Tab
包含 StackPane
,后者包含 Label
、关闭 Button
和焦点指示器。此 StackPane
具有样式类 tab-container
。
因此您可以在 CSS 中旋转此 StackPane
:
.tab-pane > .tab-header-area > .headers-region > .tab > .tab-container {
-fx-rotate:90;
}
旋转 StackPane
后我注意到,焦点指示器的处理方式不同,因此必须也旋转一个(它具有样式类 focus-indicator
) .
.tab-pane > .tab-header-area > .headers-region > .tab * .focus-indicator {
-fx-rotate:-90;
}
一个例子,和你的一样,只是添加了style-sheet,Tab
上的Label
没有旋转不再:
public class TabbedPaneTest extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("TabPane Test");
TabPane tabPane = new TabPane();
tabPane.setStyle("-fx-tab-min-height: 100");
tabPane.setRotateGraphic(true);
tabPane.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);
tabPane.getTabs().addAll(tab("Tab 1"), tab("Tab 2"));
tabPane.setSide(Side.LEFT);
Scene scene = new Scene(tabPane, 400, 200);
scene.getStylesheets().add(getClass().getResource("application.css").toString());
primaryStage.setScene(scene);
primaryStage.show();
}
static Tab tab(String labelText) {
Tab tab = new Tab();
Label label = new Label(labelText);
tab.setGraphic(new StackPane(label));
tab.setContent(new Label(" " + labelText + " content"));
return tab;
}
public static void main(String[] args) {
launch(args);
}
}
但您也可以在不使用 Tab
上的图形的情况下使用此方法:
static Tab tab(String labelText) {
Tab tab = new Tab(labelText);
tab.setContent(new Label(" " + labelText + " content"));
return tab;
}
这将生成一个 TabPane
,如图所示:
关于java - 如何在选项卡的右侧显示关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39914008/