更新 JSlider Swing 的位置
对于最近发布的大量问题,我深表歉意。
我试图让 JSlider 根据不断更新的变量来更新其位置。
setValue(n) 方法似乎不起作用。还有其他选择吗?我们用它作为音乐播放器的时间标记。
My apologies for posting tons of questions as of late.
I'm trying to get a JSlider to update its position based on a constantly updating variable.
The setValue(n) method doesn't seem to work. Is there any alternative? We're using this as a time marker for a music player.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要您正确配置了最小值和最大值,
setValue()
就可以工作。JSlider
确实需要重绘的机会,因此请确保您没有占用 事件调度线程。如果您的代码正在响应任何 UI 事件,则您位于调度线程上,并且应该尽快返回。在后台线程上执行任何昂贵的计算。有大量的优秀教程网络。
Provided that you've correctly configured a minimum and maximum value,
setValue()
will work. TheJSlider
does need an opportunity to redraw, so make sure you're not tying up the event dispatch thread. If your code is responding to any UI event, you're on the dispatch thread and should return as soon as possible.Perform any expensive calculations on a background thread. There are a plethora of excellent tutorials for this on the web.
setValue
方法确实有效,因此我会检查您传递给setValue
的值是否确实在您配置的最小值和最大值内滑块(即检查您是否传递的是百分比值而不是整数)。第二件事要注意:您应该只在事件调度线程上调用
setValue
。当然,您可以在偶数分派线程之外计算将要传递给setValue
的值,但是对 Swing 组件中的值的任何更新都应该在偶数分派线程中完成(即使用SwingUtilities.invokeLater
)The
setValue
method definitely does work, so I would check that the value that you're passing in tosetValue
is indeed within the minimum and maximum value that you've configured the slider with (ie check that you're not passing a percentage value in instead of an integer).A second thing to watch out for: you should only be calling
setValue
on the event dispatching thread. Of course you can calculate the value that is going to be passed tosetValue
outside of the even dispatching thread, but any update to a value in a Swing component should be done in the even dispatching thread (ie usingSwingUtilities.invokeLater
)