如何复制JCheckBox的禁用外观? [英] How can I replicate disabled appearance for a JCheckBox?

查看:176
本文介绍了如何复制JCheckBox的禁用外观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考以下示例代码。在Swing中有一些错误,如果组件包含HTML,则不会将禁用的组件显示为禁用。除了报告这个问题,我希望一个同事已经照顾,是否有一些好的方法来解决这个问题?

Take the following example code. There is some bug in Swing which doesn't render disabled components as disabled if the component contains HTML. Aside from reporting the issue, which I hope a colleague has already taken care of, is there some good way to work around the problem?

无论我采取什么解决方案,我想它是一个全局修复,而不是需要被黑客入应用程序中的每个复选框的东西。

Whatever solution I take, I want it to be a global fix as opposed to something that needs to be hacked into every check box in the application.

我试图为复选框创建一个自定义UI,调用 setForeground 在绘画之前和之后,但事实证明,通过调用 setForeground ,它触发一个事件,它调用 repaint(),它调用renderer,...

I tried making a custom UI for the check box which calls setForeground before and after the painting, but it turns out that by calling setForeground, it fires an event which ultimately results in it calling repaint(), which calls the renderer, ...

import java.awt.GridLayout;
import java.util.Arrays;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestCheckBoxes extends JFrame
{
    public TestCheckBoxes()
    {
        JCheckBox checkBox1 = new JCheckBox("Enabled, plain text");
        JCheckBox checkBox2 = new JCheckBox("<html><p>Enabled, HTML");
        JCheckBox checkBox3 = new JCheckBox("Disabled, plain text");
        checkBox3.setEnabled(false);
        JCheckBox checkBox4 = new JCheckBox("<html><p>Disabled, HTML");
        checkBox4.setEnabled(false);

        setLayout(new GridLayout(4, 1));

        for (JCheckBox checkBox : Arrays.asList(checkBox1, checkBox2, checkBox3, checkBox4))
        {
            checkBox.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
            add(checkBox);
        }

        ((JComponent) getContentPane()).setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        pack();
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                TestCheckBoxes frame = new TestCheckBoxes();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}


推荐答案

可以将复选框和标签分离成自己的组件,只需创建一个不带标签的复选框。您也可以将它们添加到自己的面板,并覆盖面板的 setEnabled()方法,只需启用/禁用复选框并更改标签的颜色。以这段代码为例:

You could separate the checkbox and label into their own components and simply make a checkbox without a label. You could also maybe add them to a panel of their own and override the setEnabled() method of the panel to simply enable/disable the checkbox and change the label's color. Take this code snippet for example:

final JCheckBox checkbox = new JCheckBox();
final JLabel label = new JLabel();
JPanel panel = new JPanel() {
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        checkbox.setEnabled(enabled);
        if (enabled)
            label.setForeground(Color.BLACK);
        else
            label.setForeground(Color.GRAY);
    }
};
panel.add(checkbox);
panel.add(label);

请注意,复选框在我们面板的setEnabled()`方法中使用它们。根据您将HTML插入复选框的频率,您始终可以创建自己的组件类来完成此操作。

Note that checkbox and label must be final to use them in our panel's setEnabled()` method. Depending on how often you're inserting HTML into your checkboxes, you can always create your own component class to do this as well.

public class HTMLCheckBox extends JPanel {
    private JCheckBox checkbox = new JCheckBox();
    private JLabel label = new JLabel();
    private Color disabledColor = Color.GRAY;
    private Color enabledColor = Color.BLACK;

    public HTMLCheckBox(String text) {
        label.setText(text);
        add(checkbox);
        add(label);
    }

    public boolean isSelected() {
        return checkbox.isSelected();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        checkbox.setEnabled(enabled);
        if (enabled)
            label.setForeground(enabledColor);
        else
            label.setForeground(disabledColor);
    }
}

然后添加自己的构造函数和方法欲望。例如,重载 setBackground(),让它为面板,复选框和标签设置背景。更改标签文本的 setText()方法可能也很方便。无论你想要什么。甚至可以为 enabledColor disabledColor 设置允许您随意更改这些。

And then add your own constructors and methods as you so desire. For example, override setBackground() to have it set the background for the panel, checkbox, and label. A setText() method to change the label text would probably also be convenient. Whatever you'd want it to do. And maybe even setters for enabledColor and disabledColor to allow you to change these at will.

这篇关于如何复制JCheckBox的禁用外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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