How to make JavaFX Slider to move in discrete steps?(如何使 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())));
这篇关于如何使 JavaFX Slider 以离散的步骤移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使 JavaFX Slider 以离散的步骤移动?
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
