我正在尝试为一个网络应用程序开发GUI,希望设置一个TabPane,在左侧放置选项卡并保持选项卡标题水平。
我已经找到了如何将选项卡放置在左侧,但是在许多搜索之后,我没有成功地设置标题的正确对齐方式。它们仍然垂直且难以阅读。
我该怎么解决这个问题呢?
我正在尝试为一个网络应用程序开发GUI,希望设置一个TabPane,在左侧放置选项卡并保持选项卡标题水平。
我已经找到了如何将选项卡放置在左侧,但是在许多搜索之后,我没有成功地设置标题的正确对齐方式。它们仍然垂直且难以阅读。
我该怎么解决这个问题呢?
您可以使用CSS来自定义选项卡和选项卡标签:
.tab *.tab-label {
-fx-rotate: 90;
}
.tab {
-fx-padding: 3em 0.5em 3em 0.5em;
}
void setTabPaneLeftTabsHorizontal(TabPane tabPane){
tabPane.setSide(Side.LEFT);
tabPane.setRotateGraphic(true);
Label l = null;
StackPane stp = null;
for(Tab t : tabPane.getTabs()){
l = new Label(t.getText());
l.setRotate(90);
stp = new StackPane(new Group(l));
stp.setRotate(90);
t.setGraphic(stp);
t.setText("");
}
tabPane.setTabMinHeight(100);
tabPane.setTabMaxHeight(100);
}
哦,我记得我之前遇到过这个问题,只需在tabHeader中添加一个StackPane、VBox或Hbox,并添加您可能需要的所有组件,它们将不会遵循选项卡方向。
private StackPane createTabHeader(String text, Node graphics){
return new StackPane(new Group(new Label(text, graphics)));
}
Tab tab = new Tab();
tab.setGraphic(createTabHeader("TEXT", new ImageView("IMAGE_PATH")));
TabPane tabPane = new TabPane(tab);
tabPane.setSide(Side.LEFT);
tabPane.setTabMinWidth(50);
tabPane.setTabMaxWidth(50);
tabPane.setTabMinHeight(200);
tabPane.setTabMaxHeight(200);
// Firstly
tabPane.setSide(Side.LEFT);
tabPane.setRotateGraphic(true);
Label l = new Label("Titel Tab1");
l.setRotate(90);
StackPane stp = new StackPane(new Group(l));
stp.setRotate(90);
tab1.setGraphic(stp);
l = new Label("Titel Tab2");
l.setRotate(90);
stp = new StackPane(new Group(l));
stp.setRotate(90);
tab2.setGraphic(stp);
tabPane.setTabMinHeight(100);
tabPane.setTabMaxHeight(100);
还有另一种解决方案,可以在 CSS 中设置最小宽度和高度,并在 FXML 中添加图形元素。
-fx-tab-min-width:30;
-fx-tab-max-width:30;
-fx-tab-min-height:150;
-fx-tab-max-height:150;
<Tab closable="false">
<graphic>
<Pane prefHeight="76.0" prefWidth="30.0">
<children>
<Label layoutX="-35.0" layoutY="23.0" prefHeight="30.0" prefWidth="130.0" text="User">
<font>
<Font name="SansSerif Regular" size="14.0" />
</font></Label>
</children>
</Pane>
</graphic>
</Tab>