swing:JSlider 但具有粗/细控制?
我有一个 JSlider,有 65536 个不同的值。它非常适合粗调和非常精细的调整(使用向上/向下箭头+/-1),但在中间效果很差。
还有什么更好的吗?我可以模糊地想象使用两个滑块,一个用于粗调+微调,但无法真正弄清楚如何让它们一起工作。
I have a JSlider with 65536 different values. It works great for coarse adjustments and for very fine adjustments (+/-1 using up/down arrow) but is very poor in the middle.
Is there anything out there that would be better? I can vaguely imagine taking 2 sliders one for coarse + fine adjustments, but can't really figure out how to get them to work together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
JSpinner
而不是JSlider
怎么样?使用SpinnerNumberModel
,您可以设置步长,甚至可以动态更改步长。如果您同意拥有多个控件,您甚至可以拥有两个微调器,一个用于设置值,另一个用于设置第一个微调器使用的步长。
作为一个例子,我采用了 SliderDemo Swing 滑块教程 中的代码并将其修改为使用两个 JSpinners 而不是单个 JSlider。这是我更改的代码中最有趣的部分:
我还必须进行一些不太有趣的更改,例如为步长微调器创建标签,将新标签和新微调器添加到容器,以及更改 <
this
上的 code>stateChanged() 方法将事件源强制转换为JSpinner
,而不是强制转换为JSlider
。当然,您可以进一步详细说明,例如增加步长微调器的步长(例如,只需单击一次即可将步长从 1 更改为 101)。您还可以使用不同的控件而不是
JSpinner
来设置步长,例如组合框。最后,为了使这一切真正易于使用,您可能需要连接一些按键加速器(可能通过菜单?),以便您可以更改步长,而无需实际将鼠标或键盘焦点从一个微调器移动到另一个微调器。
编辑:考虑到无论如何你都必须使用
JSlider
,你是否知道你可以使用 PgUp/PgDn 上下移动 1/10总范围的第 th?如果您想更改 1/10th 数量(例如使其动态),则需要重写方法
BasicSliderUI.scrollByBlock()
。在下面的示例中,我刚刚覆盖了
JSlider
的 UI 类,以按范围的 1/4 步进,而不是 1/10th >:从这里开始,用另一个滑块或微调器设置的变量替换该常量
SLIDER_FRACTION
不会太难,不是吗?What about using a
JSpinner
instead of aJSlider
? With aSpinnerNumberModel
, you can set the step size and even change the step size dynamically.If you're OK with having multiple controls, you could even have two spinners, one for setting your values and another for setting the step size that is used by the first spinner.
For an example of this, I took the SliderDemo code from the Swing slider tutorial and modified it instead to use two
JSpinners
instead of a singleJSlider
. Here's the most interesting part of the code that I changed:I also had to make a bunch of less interesting changes, such as creating a label for the step size spinner, adding the new label and new spinner to the container, and changing the
stateChanged()
method onthis
to cast the source of the event to aJSpinner
instead of casting it to aJSlider
.You could, of course, elaborate on this further, such as increasing the step size for the step size spinner (for example, so that you can change the step size from 1 to 101 in a single click). You could also use a different control instead of a
JSpinner
to set the step size, such as a combo box.Finally, to make this all really easy to use, you would likely want to hook up some keystroke accelerators (possibly through a menu?) so that you could change the step size without actually moving the mouse or the keyboard focus from one spinner to another.
Edit: Given that you have to use a
JSlider
no matter what, are you aware that you can use PgUp/PgDn to move up and down by 1/10th of the total range?If you want to change that 1/10th amount (such as making it dynamic), then you'll need to override the the method
BasicSliderUI.scrollByBlock()
.Here's an example where I just overrode the UI class of a
JSlider
to step by 1/4th of the range, instead of 1/10th:From here, it wouldn't be too hard to replace that constant
SLIDER_FRACTION
with a variable that was set by another slider or by a spinner, would it?