删除JSlider中通过拇指显示的值 [英] Remove value displaying over thumb in JSlider

查看:122
本文介绍了删除JSlider中通过拇指显示的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSlider有点问题,我无法解决。为了解释一下这种情况,我必须做一个从0到20的JSlider,通过0.1步。我通过创建一个从0到200的JSlider来解决问题,并重新定义了滑块下的标签以显示相应的百分比而不是整数值。

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.

但是我有一个最后的问题:我正在使用自定义L& F(我无法更改,显然,因为它来自客户端),它在滑块上显示值。但是,此值以标准方式显示为整数。从我能够掌握的,这个显示与 Slider.paintValue 属性相关,我可以在 javax.swing.plaf中看到.synth.SynthSliderUI 源代码。但是,我一直无法将其删除。

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.

我现在尝试过:

UIManager.getLookAndFeelDefaults().put("Slider.paintValue", false);
UIManager.put("Slider.paintValue", false);

这两个都没有改变任何东西。

None of those two changed anything.

周围有一个Swing大师可以让我离开吗?

Is there a Swing guru around here who could get me out of this ?

推荐答案

我刚刚用<$创建了一个演示c $ c> JSlider 并使用基本的Synth LAF,我可以确认设置这些属性似乎根本没有效果,这很奇怪,而且非常烦人。

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.

我查看了 SynthSliderUI ,我可以看到 protected boolean paintValue 正在设置在 private void updateStyle(JSlider c)中,所以如果您要为此类创建自定义扩展,请设置 paintValue 为false,然后使用 setUI()将滑块的UI设置为新UI,您可能会更接近。或者完全覆盖绘制方法,将代码复制出原始代码,但删除绘制拇指值的部分。

I have looked at SynthSliderUI and I can see the protected boolean paintValue being set in private void updateStyle(JSlider c), so if you were to make a custom extension this class, set the value of paintValue to false, and then set your slider's UI to the new UI with setUI(), 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.

现在, SynthSliderUI 本身是包私有的,但由于您使用的是客户端提供的LAF,因此无论如何这都不是问题。如果他们的自定义滑块UI类是公共和非最终的,你可以像我上面描述的那样制作一个黑客版本,然后将它应用到你的滑块。

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.

如果它是最终的,然后你可以使用反射来改变 paintValue 字段作为最后的手段(也许就在绘制滑块之前)。以下hack适用于我(取出反射调用,将字段设置为false以查看正在绘制的拇指值):

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):

import java.awt.BorderLayout;
import java.lang.reflect.Field;

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class SliderDemo {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");

        Class<?> sliderUIClass = Class.forName("javax.swing.plaf.synth.SynthSliderUI");
        final Field paintValue = sliderUIClass.getDeclaredField("paintValue");
        paintValue.setAccessible(true);

        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLayout(new BorderLayout());

                JSlider slider = new JSlider(3, 6, 4);
                try {
                    paintValue.set(slider.getUI(), false);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                f.add(slider, BorderLayout.CENTER);

                f.pack();
                f.setVisible(true);
            }
        });
    }

}

这篇关于删除JSlider中通过拇指显示的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆