更改所选JToggleButton的背景颜色 [英] Changing the background color of a selected JToggleButton

查看:265
本文介绍了更改所选JToggleButton的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以可靠,独立于外观的方式选择它时,我试图改变 JToggleButton 的颜色。

I am trying to change the color of a JToggleButton when it has been selected in a reliable, look and feel independent way.

如果使用Metal L& F,那么使用UIManager是一种方法:

If using the Metal L&F, then using the UIManager is an approach:

UIManager.put("ToggleButton.selected", Color.RED);

注意:Iyy指出我的房产名称中有拼写错误以上,但我会把它留给上面的人来到这里,但实际的属性名称应该是:

Note: Iyy pointed out that I had a typo in the property name above, but I will leave it above for people getting here, but the actual property name is supposed to be:

UIManager.put("ToggleButton.select", Color.RED);

但是,这在我目前的外观(目前是Windows XP)中不起作用。经过一些进一步的分析,看来Windows(仍然是XP)中的系统外观并没有使用任何 Color - 基于 UIManager ToggleButton 的属性,或者它至少不提供它们(在线快速示例 UIManager 中查找所有属性键,其中包含示例很方便地明确限制为 Color 属性。)

However, this does not work in my current Look and Feel (currently Windows XP). After some further analysis, it appears that the system look and feel in Windows (still XP) does not use any of the Color-based UIManager properties for ToggleButton at all, or it at least does not supply them itself (there is a quick example online to find all property keys from the UIManager, which in the example is conveniently limited explicitly to Color properties).

我尝试设置背景颜色:

Action action = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) { /* stuff */ }
};
JToggleButton button = new JToggleButton(action);
// tried with and without opaque true
button.setOpaque(true);
button.setBackground(Color.RED);

它不仅不会改变所选状态,而且甚至不会影响未选择状态。

Not only does it not change the selected state, but that does not even effect the unselected state.

我在尝试接收动作后尝试更改背景颜色:

I have tried changing the background color only after receiving the action:

@Override
public void actionPerformed(ActionEvent e)
{
    JToggleButton button = (JToggleButton)e.getSource();
    if (button.isSelected()) // alternatively, (Boolean)getValue(Action.SELECTED_KEY)
    {
        button.setBackground(Color.RED);
    }
}

这些都不起作用。我找到的唯一工作要求我自己在选定状态下绘制按钮(这导致一个工作示例,虽然看起来非标准):

None of that works. The only thing that I have found to work requires me to draw the button myself in the selected state (which leads to a working example, albeit non-standard looking):

private class ColoredToggleButton extends JToggleButton
{
    ColoredToggleButton(Action action, Color color)
    {
        super(action);

        setBackground(color);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (this.isSelected())
        {
            int w = getWidth();
            int h = getHeight();
            String s = getText();

            // selected color
            g.setColor(getBackground());
            g.fillRect(0, 0, w, h);
            // selected foreground color
            g.setColor(SystemColor.controlText);
            g.drawString(s,
                         (w - g.getFontMetrics().stringWidth(s)) / 2 + 1,
                         (h + g.getFontMetrics().getAscent()) / 2 - 1);
        }
    }
}

稍微修改一下在此 Java错误报告中发表评论。有趣的是(有趣的?),声称已于1998年修复。

That is slightly modified from a comment in this Java bug report. Interestingly (amusingly?), in claims to have been fixed in 1998.

有没有人知道更好的L& F独立方式来设置所选的背景颜色JToggleButton?

Does anyone know of a better, L&F independent way to set the background color of a selected JToggleButton?

推荐答案

你可能会看到 setIcon()是否足够您的目的,但您也可以在 paint() /swing/plaf/ButtonUI.htmlrel =nofollow noreferrer> ButtonUI 委托。

You might see if setIcon() is sufficient for your purpose, but you can also override paint() in the ButtonUI delegate.

附录:@ kleopatra的评论很好:改变UI代表是微不足道的。 @ mKorbel最近的示例显示了该方法的难度和多功能性。它的主要优点是外观和感觉独立。

Addendum: @kleopatra's comment is well-taken: changing the UI delegate is not trivial. @mKorbel's recent example shows both the difficulty and versatility of the approach. Its essential advantage is look & feel independence.

提到了一些不太雄心勃勃的方法这里

Some less ambitious approaches are mentioned here.

这篇关于更改所选JToggleButton的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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