与 JSlider 一起使用的自定义范围
我正在尝试创建一个在以下范围内移动的 jslider。
[-x,-1)[1,x]
简而言之,我不希望值 -1 和 0 成为 JSlider 的有效值。 但应允许从 x 到 -1 的值,并且应允许 1 到 x。
我试图不编写 hacky 代码,所以我不想在 UI 代码中编写一个函数,该函数只是从不同的(连续)范围获取值,然后用一堆将其转换为我想要的范围它声明。
理想情况下,我应该能够调用 slider.getValue()
并知道返回值将在我上面描述的范围内。
I'm trying to create a jslider that moves withing the following ranges.
[-x,-1)[1,x]
In short I don't want the values -1 and 0 to be valid values for the JSlider.
But the values from x to -1 should be allowed, and 1 to x should be allowed.
I'm trying to not write hacky code, so I don't want to write a function in the UI code that just gets the value from a different (continuous) range, and then transforms it to the range I want with a bunch of it statements.
Ideally, I should just be able to call slider.getValue()
and know that the return value will be in the range I described above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您必须在重写的方法
setValue()
中自己进行此值调整?试试这个代码:
I think you must do this value adjustment yourself perhaps within overridden method
setValue()
?Try out this code:
滑块的值只是拇指当前位置与滑块宽度之间的比率(以像素为单位)。 IIUC,您有三个范围,
[-x,-1)
、[-1,1)
和[1,x]
,所以你需要两个拇指。您可以查看JXMultiThumbSlider
,它支持MultiThumbModel
。附录:对于相关的用例,我从 如何编写自定义 Swing 组件。这很费力,但可能会产生更干净的结果。
A slider's value is just the ratio between the thumb's current position and the sliders width, in pixels. IIUC, you have three ranges,
[-x,-1)
,[-1,1)
and[1,x]
, so you'll need two thumbs. You might look atJXMultiThumbSlider
, which supports aMultiThumbModel
.Addendum: For a related use-case, I started with How to Write a Custom Swing Component. It's laborious, but it may produce a cleaner result.