Java 7 JColorChooser:禁用透明度滑块 [英] Java 7 JColorChooser: Disable Transparency Slider

查看:272
本文介绍了Java 7 JColorChooser:禁用透明度滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JDK 7为 JColorChooser添加了一个新的透明度滑块

问题在于我不想让我的用户选择透明色。不幸的是,似乎没有一种简单的方法可以禁用滑块。

The problem is that I do not want to allow my users to pick transparent colors. Unfortunately, there doesn't seem to be an easy way to disable the slider.

摆脱透明度的一种方法是创建一个基于选择一个但删除alpha值。然而,这给用户留下了错误的印象,因为滑块现在实际上什么都不做,我讨厌有一个无用的UI元素。

One way to get rid of the transparency is to just create a new color based on the selected one but removing the alpha value. However, this gives a false impression to the user as the slider now effectively does nothing and I would hate to have a useless UI element around.

所以我的问题是,什么是摆脱透明度滑块的最佳方法是什么?

So my question is, what's the best way to get rid of the transparency slider?

PS:IMO,他们只是添加滑块并使其成为默认行为,这很奇怪。这可能会导致JDK 6程序中的许多错误,这些错误不希望颜色选择器返回带有alpha值的颜色。

P.S.: IMO, it's weird that they would just add the slider and make it the default behavior. This might cause a lot of bugs in JDK 6 programs that do not expect the color chooser to return a color with an alpha value.

推荐答案

根据文档,可以只修改/配置现有的类。因此,建议的方法是创建自己的ChooserPanel(他们需要扩展 AbstractColorChooserPanel ),然后调用

According to the documentation, it's possible to just modify/configure the existing classes. So recommended way to go is therefore to create your own ChooserPanels (they need to extend AbstractColorChooserPanel) and then invoke

JColorChooser jc = new JColorChooser();
jc.setChooserPanels(new AbstractColorChooserPanel[]{yourChooserPanel});

或者,如果您正在寻找更快/更凶悍/更丑陋的方式来做这件事,请写下这个你:

Alternatively, if yor aree looking for a faster/nastier/uglier way to do it, wrote this for you:

private static void removeTransparencySlider(JColorChooser jc) throws Exception {

    AbstractColorChooserPanel[] colorPanels = jc.getChooserPanels();
    for (int i = 1; i < colorPanels.length; i++) {
        AbstractColorChooserPanel cp = colorPanels[i];

        Field f = cp.getClass().getDeclaredField("panel");
        f.setAccessible(true);

        Object colorPanel = f.get(cp);
        Field f2 = colorPanel.getClass().getDeclaredField("spinners");
        f2.setAccessible(true);
        Object spinners = f2.get(colorPanel);

        Object transpSlispinner = Array.get(spinners, 3);
        if (i == colorPanels.length - 1) {
            transpSlispinner = Array.get(spinners, 4);
        }
        Field f3 = transpSlispinner.getClass().getDeclaredField("slider");
        f3.setAccessible(true);
        JSlider slider = (JSlider) f3.get(transpSlispinner);
        slider.setEnabled(false);
        Field f4 = transpSlispinner.getClass().getDeclaredField("spinner");
        f4.setAccessible(true);
        JSpinner spinner = (JSpinner) f4.get(transpSlispinner);
        spinner.setEnabled(false);
    }
}

祝你好运:)

这篇关于Java 7 JColorChooser:禁用透明度滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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