【问题标题】:Change the color of a JButton in JGoodies WindowsLookAndFeel在 JGoodies WindowsLookAndFeel 中更改 JButton 的颜色
【发布时间】:2015-12-12 07:45:18
【问题描述】:

如果我使用 JGoodies WindowsLookAndFeel,如何更改 JButton 的颜色?更改颜色后,单击按钮时仍应有一些视觉指示;颜色渐变和点击动画不必与 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);
    }
}

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

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

【问题讨论】:

  • 你用的是什么windows版本?
  • 我使用的是 Windows 8

标签: java swing look-and-feel jgoodies


【解决方案1】:

尝试添加对 setContentAreaFilled 的调用:

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

或者您可以覆盖 JButton 并绘制: Change JButton gradient color, but only for one button, not all

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 2019-02-14
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多