如何编辑 JFXSpinner 内部文本
How to edit JFXSpinner inner text
是否可以更改 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);
}
}
我不得不去源代码
JFXSpinnerSkin.java 并发现文本对象具有样式类:text 或 percentage,因此在css 您必须执行以下操作:
.jfx-spinner .percentage
{
-fx-stroke: white;
}
注意: 如果您使用的是 JavaFX Scene Builder 或类似工具,我建议您在 "Style Class" 字段中添加这些类名。
是否可以更改 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);
}
}
我不得不去源代码 JFXSpinnerSkin.java 并发现文本对象具有样式类:text 或 percentage,因此在css 您必须执行以下操作:
.jfx-spinner .percentage
{
-fx-stroke: white;
}
注意: 如果您使用的是 JavaFX Scene Builder 或类似工具,我建议您在 "Style Class" 字段中添加这些类名。