5

如果我使用 JGoodies,如何更改 JButton 的颜色WindowsLookAndFeel?更改颜色后,单击按钮时仍应有一些视觉指示;颜色渐变和点击动画不必与 JGoodies 中的相同。

使用setBackground()andsetForeground()只改变按钮轮廓和按钮文本的颜色:

import com.jgoodies.looks.windows.WindowsLookAndFeel;
...

public class Test {
    public static void main(String[] args) throws UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(new WindowsLookAndFeel());

        JFrame frame = new JFrame();
        frame.setSize(50, 100);
        JButton button = new JButton("Button");
        button.setBackground(Color.GREEN);
        button.setForeground(Color.RED);
        button.setOpaque(true);
        frame.add(button);
        frame.setVisible(true);
    }
}

在此处输入图像描述

我想为按钮的整个区域设置颜色,而不仅仅是轮廓。(如果WindowsLookAndFeel不使用,就会发生这种情况。)

我也尝试过更改颜色,com.jgoodies.looks.windows.WindowsBorders#getButtonBorder()但这似乎没有任何效果。

4

1 回答 1

1

尝试添加对 setContentAreaFilled 的调用:

button.setContentAreaFilled(false); //must be before setOpaque
button.setOpaque(true);

或者您可以覆盖 JButton 并绘制: 更改 JButton 渐变颜色,但仅限于一个按钮,而不是全部

于 2015-09-29T15:41:22.283 回答