如何在 JavaFX 中设置 Segmented Bar Segments 的颜色?

How to set colours of Segmented Bar Segments in JavaFX?

如何在来自 ControlsFX 的 JavafX 中设置分段条的特定段的颜色?

https://javadoc.io/static/org.controlsfx/controlsfx/8.40.16/org/controlsfx/control/SegmentedBar.html

所以如果我非常基本地构建并设置如下两个段,我将如何将它们设置为不同的颜色?

@FXML SegmentedBar segments;

public void setSegments() {
segments.getSegments().addAll(
     new SegmentedBar.Segment(10, "10"),
     new SegmentedBar.Segment(90, "90"));
}

我在其他地方看到的 .setStyle() 方法似乎只适用于 Segmented Bar 对象本身,而不适用于各个段。所以我很茫然。

使用 segmentViewFactory 设置样式 SegmentView 用于显示每个 Segment:

import org.controlsfx.control.SegmentedBar;
import org.controlsfx.control.SegmentedBar.Segment;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;



public class App extends Application {

    @Override
    public void start(Stage stage) {
        SegmentedBar<Segment> segments = new SegmentedBar<>();
        segments.setOrientation(Orientation.HORIZONTAL);
        segments.getSegments().addAll(
                 new SegmentedBar.Segment(10, "10"),
                 new SegmentedBar.Segment(90, "90")     
        );
        segments.setSegmentViewFactory(segment -> {
            SegmentedBar<Segment>.SegmentView view = segments.new SegmentView(segment);
            String color = segment.getValue() < 50 ? "#66C2A5" : "#FC8D62" ;
            view.setStyle("-fx-background-color: "+color);
            return view ;
        });
        
        BorderPane root = new BorderPane(segments);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 800, 500);
        stage.setScene(scene);
        stage.show();
    }

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

}