如何让JavaFX Slider以不连续的步骤移动? [英] How to make JavaFX Slider to move in discrete steps?
问题描述
我使用 JavaFx
制作 GUI
,我需要只允许整数的滑块
永远被选中。
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
然后你可以设置 Number 值的api / java / lang / Number.html#intValue--rel =noreferrer>整数值:
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())));
这篇关于如何让JavaFX Slider以不连续的步骤移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!