是否可以更改 JFXSpinner
进度文本的显示方式。我不希望它显示为百分比,而是显示为 0 到 1 之间的索引。
最佳答案
因此,我通过子类化 JFXSpinnerSkin
并覆盖 layoutChildren
取得了成功。我使用 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);
}
}
关于java - 如何编辑 JFXSpinner 内部文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49975777/