自定义Nimbus风格的JButton

3

你好,我正在尝试定制Nimbus中的JButton,以下是我的操作:

public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    Font police1 = new Font("Tahoma", Font.BOLD, 12);
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                UIManager.getLookAndFeelDefaults().put("Button.background",new Color(18,55,63));
                UIManager.getLookAndFeelDefaults().put("Button.font", police1);
                UIManager.getLookAndFeelDefaults().put("Button.textForeground", new Color(122,216,247));

                break;
            }
        }
    } catch (Exception e) {
        // If Nimbus is not available, you can set the GUI to another look    and feel.
}

下面是我得到的:

buttons

您可以看到每个按钮周围都有一些灰色边框,我想要去除它或者改变它的颜色,但我找不到方法 :(。

当我按下其中一个按钮时,会得到这个:

button pressed

实际上,这是距离颜色(18,55,63)最近的一个。请问是否有办法分别自定义正常外观和按下时的外观?我查阅了此链接,但对我没有帮助。

2个回答

5

每个按钮周围都有一种灰色边框,我想要去掉它或者改变它的颜色,但是我找不到方法

要去掉 JButton 的边框,只需调用 JButton.setBorderPainted(boolean)。或者您可以创建一个符合您需求的自定义 Border 并调用 JButton.setBorder(Border)


1
感谢您的回答,但是不幸的是 Button.setBorderPainted(false) 没有改变任何东西,它可能不适用于 Nimbus 按钮。 :( - Exia0890
@Exia0890,请查看XxxPainters的按钮,另一种方法可能是使用Nimbus默认值中的一个,但也会与另一个JComponents竞争,问题仅在JButton周围的边框是否有问题? - mKorbel
我明白了。唉,Nimbus...在决定删除这篇文章之前,我仍在寻找一些可能性,因为它与问题完全不相关。 - David Kroukamp

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接