如何使 JavaFX Slider 以离散的步骤移动?
我正在使用 JavaFx
制作一个 GUI
,我需要只允许选择 integers
的滑块.
I am making a GUI
using JavaFx
and I need sliders that only allow integers
to ever be selected.
我知道我可以使用 snapToTicks
,但在拉出 "knob"
时,它仍然可以表示 非整数
值.我想摆脱它.它会弄乱与之链接的其他组件.
I know I can use snapToTicks
, but while pulling the "knob"
, it can still represent a non-integer
value. I would like to get rid of that. It messes up other components linked to it.
基本上,我想要 Swing 的 JSlider
之类的东西,但要使用 JavaFx
.是否可以?我一直在寻找,但找不到任何东西.
Basically, I want something like Swing's JSlider
, but with JavaFx
. Is it possible? I have been searching but I can't find anything.
推荐答案
你可以简单地添加一个监听器到 valueProperty
的 Slider
然后你可以设置 新 的整数值编号
值:
You can simply add a listener to the valueProperty
of the Slider
and then you can either set the integer value of the new Number
value:
slider.valueProperty().addListener((obs, oldval, newVal) ->
slider.setValue(newVal.intValue()));
或者您可以使用整数舍入 Math.round
:
or alternatively you can use integer rounding using Math.round
:
slider.valueProperty().addListener((obs, oldval, newVal) ->
slider.setValue(Math.round(newVal.doubleValue())));
相关文章