在JGoodies WindowsLookAndFeel中更改JButton的颜色

Chr*_*s K 5 java swing look-and-feel jgoodies

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

使用setBackground()setForeground()仅更改按钮轮廓和按钮文本的颜色:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

我也试过改变颜色,com.jgoodies.looks.windows.WindowsBorders#getButtonBorder()但这似乎没有任何影响.

Vov*_*vka 1

尝试添加对 setContentAreaFilled 的调用:

button.setContentAreaFilled(false); //must be before setOpaque
button.setOpaque(true);
Run Code Online (Sandbox Code Playgroud)

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