删除 JSlider 中拇指上显示的值
我的 JSlider 有一个小问题,我无法解决。为了稍微解释一下这种情况,我必须做一个 JSlider,从 0 到 20,通过 0.1 步长。我通过创建一个从 0 到 200 的 JSlider 来解决问题,并重新定义滑块下的标签以显示正确的百分比而不是整数值。
但我有最后一个问题:我正在使用自定义 L&F(显然,我无法更改它,因为它来自客户端),它在滑块拇指上显示值。但是,该值以标准方式显示为整数。据我所知,此显示与 Slider.paintValue
属性相关,正如我在 javax.swing.plaf.synth.SynthSliderUI
源代码中看到的那样代码。然而,我一直无法删除它。
我现在尝试过的:
UIManager.getLookAndFeelDefaults().put("Slider.paintValue", false);
UIManager.put("Slider.paintValue", false);
这两个都没有改变任何东西。
这里有一位 Swing 大师可以帮助我摆脱困境吗?
I have a little problem with my JSlider, that I haven't been able to solve. To explain the situation a little, I have to do a JSlider going from 0 to 20, through 0.1 steps. I tricked my way out of problems by creating a JSlider from 0 to 200, and redefined the labels under the slider to display the corrent percentages instead of the integer values.
But I have one last problem : I'm using a custom L&F (that I can't change, obviously, as it is from the client), that displays the value over the slider thumb. However, this value is displayed...in its standard way, as an integer. From what I've been able to grasp, this display is related to the Slider.paintValue
property, as I can see in javax.swing.plaf.synth.SynthSliderUI
source code. However, I've been dramatically unable to remove it.
What I've tried for now :
UIManager.getLookAndFeelDefaults().put("Slider.paintValue", false);
UIManager.put("Slider.paintValue", false);
None of those two changed anything.
Is there a Swing guru around here who could get me out of this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚使用 JSlider 并使用基本的 Synth LAF 创建了一个演示,我可以确认设置这些属性似乎根本没有效果,这很奇怪,也很烦人。
我查看了
SynthSliderUI
,我可以看到protected boolean PaintValue
被设置在private void updateStyle(JSlider c)
中,所以如果你要在此类中创建一个自定义扩展,将paintValue
的值设置为 false,然后使用setUI()
将滑块的 UI 设置为新 UI,您可能会更接近。或者完全重写绘制方法,从原始代码中复制代码,但删除绘制拇指值的部分。现在,SynthSliderUI 本身是包私有的,但这并不是真正的问题,因为您使用的是客户端提供的 LAF。如果他们的自定义滑块 UI 类是公共的且非最终版本,您可以按照我上面的描述制作一个破解版本,并将其应用到您的滑块。
如果它是最终的,那么您也许可以使用反射来更改
paintValue
字段作为最后的手段(也许就在绘制滑块之前)。以下黑客对我有用(取出将字段设置为 false 的反射调用以查看正在绘制的拇指值):I just created a demo with a
JSlider
and using the basic Synth LAF, and I can confirm that setting those properties appears to have no effect at all, which is odd, and very annoying too.I have looked at
SynthSliderUI
and I can see theprotected boolean paintValue
being set inprivate void updateStyle(JSlider c)
, so if you were to make a custom extension this class, set the value ofpaintValue
to false, and then set your slider's UI to the new UI withsetUI()
, you might get closer. Either that, or completely override the paint methods, copying code out of the original but removing the part that paints the thumb value.Now,
SynthSliderUI
itself is package-private, but that isn't really the issue anyway since you're using a client-provided LAF. If their custom slider UI class is public and non-final, you can make a hacked version as I described above, and apply it to your slider.If it's final, then you might be able to use reflection to change that
paintValue
field as a last resort (maybe just before painting your slider). The following hack works for me (take out the reflection call that sets the field to false to see the thumb value being painted):在调用
initComponents()
之前调用UIManager.put("Slider.paintValue", false)
对我有用。Calling
UIManager.put("Slider.paintValue", false)
before the call toinitComponents()
worked for me.