创建具有定义的最小制表符宽度的制表符窗格时,JavaFX将使每个制表符的标签居中。

有没有办法使标签左对齐?我已经试过了:

package prv.rli.codetest;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;

public class TabPaneFixedWidthLeftAligned extends Application {

    public TabPaneFixedWidthLeftAligned() {
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        TabPane pane = new TabPane();
        pane.setTabMinWidth(150);
        pane.getTabs().addAll(new Tab("Bilbo"), new Tab("Baggins"));
        pane.getStylesheets().add("file:///home/rli/devel/repository/source/java/project/apps/CodeTest/src/main/java/prv/rli/codetest/test.css");
        Scene scene = new Scene(pane, 640, 480);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

与此CSS一起:
.tab-container {
    -fx-background-color: red;
    -fx-alignment: center-left;
}

.tab-label {
    -fx-background-color: blue;
    -fx-alignment: center-left;
    -fx-text-alignment: left;
}

尽管背景颜色有效(表明我走在正确的轨道上),但我无法影响标签的文本对齐方式。

最佳答案

我通过设置以下属性实现了这一点:

.tab-pane
{
    -fx-tab-min-width: 150.0px;
}

.tab .tab-label
{
    -fx-alignment: center;
}

另请参见StackOverflow上的以下答案:Change JavaFx Tab default Look

07-24 18:58
查看更多