删除 JSlider 中拇指上显示的值

发布于 2024-10-08 09:54:39 字数 556 浏览 7 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

献世佛 2024-10-15 09:54:40

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

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

现在,SynthSliderUI 本身是包私有的,但这并不是真正的问题,因为您使用的是客户端提供的 LAF。如果他们的自定义滑块 UI 类是公共的且非最终版本,您可以按照我上面的描述制作一个破解版本,并将其应用到您的滑块。

如果它是最终的,那么您也许可以使用反射来更改 paintValue 字段作为最后的手段(也许就在绘制滑块之前)。以下黑客对我有用(取出将字段设置为 false 的反射调用以查看正在绘制的拇指值):

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);
            }
        });
    }

}

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 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.

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

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);
            }
        });
    }

}
北城半夏 2024-10-15 09:54:40

在调用 initComponents() 之前调用 UIManager.put("Slider.paintValue", false) 对我有用。

Calling UIManager.put("Slider.paintValue", false) before the call to initComponents() worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文