1

是否可以更改JFXSpinner进度文本的显示方式。我不希望它显示为百分比,而是显示为 0 到 1 之间的索引。

4

2 回答 2

1

所以我通过子类化JFXSpinnerSkin和覆盖成功了layoutChildren。我得到 Text 节点Node#lookup()并更改它的文本。由于文本不再居中,我检查了源代码,得到了居中线并对其进行了调整。

public class SpinnerCustomSkin extends JFXSpinnerSkin {
    private SpinnerCustom control;
    private DecimalFormat formatter = new DecimalFormat("0.00");

    public SpinnerCustomSkin(SpinnerCustom control) {
        super(control);

        this.control = control;
    }

    @Override
    protected void layoutChildren(double contentX, double contentY, double contentWidth, double contentHeight) {
        super.layoutChildren(contentX, contentY, contentWidth, contentHeight);

        Text text = ((Text) getNode().lookup("Text"));
        text.setText(formatter.format(control.getProgress())); //Or whatever you want to display
        text.relocate((control.getRadius() - text.getLayoutBounds().getWidth()) / 2, (control.getRadius() - text.getLayoutBounds().getHeight()) / 2);
    }
}

最后,我只是对JFXSpinner元素进行了子类化,也许可以通过另一种方式设置皮肤,但我没有找到(实际上我没有搜索这么久)。

public class SpinnerCustom extends JFXSpinner {
    @Override
    protected Skin<?> createDefaultSkin() {
        return new SpinnerCustomSkin(this);
    }
}
于 2018-04-26T13:26:22.983 回答
0

我不得不去 JFXSpinnerSkin.java的源代码,发现文本对象有 styleClass: textpercent,所以在 css 中你必须执行以下操作:

.jfx-spinner .percentage
{
    -fx-stroke: white;
}

注意:如果您使用 JavaFX Scene Builder 或类似工具,我建议您在“样式类”字段中添加这些类名。

于 2018-12-02T03:59:41.370 回答