设置外观颜色 [英] Set look and feel color

查看:82
本文介绍了设置外观颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nimbus Look&感觉在我的Java Swing应用程序中. L& F看起来不错,但我需要更改一些设置(字体,颜色等)以适合我公司的企业标识.

以下代码设置整个应用程序的L& F:


该代码可以执行它应该执行的操作(设置Nimbus外观和感觉). 问题是customizeNimbusLaF()无法正常工作,正如我期望的那样.


根据要设置的属性,UIConstants中常量的数据类型为FontColor类型.

问题是,只有少数外观&感觉选择改变了.诸如树的背景之类的选项不会根据外观上的选项而更改.感觉.我知道这一点,只有当我更改组件的绘制程序时,有些事情才会更改.但是例如JPanel没有这样的painter属性,但是也会产生问题.

我还有一个关于外观的第二个问题.感觉: 画家不是menuBar应该使用在原色部分中设置的颜色,还是为此需要实现自己的绘画工具?

有人可以告诉我我的问题在哪里吗?

解决方案

1.更改设置有点古怪,必须设置L& F,然后才可以对已安装的L& F进行任何更改

伪代码

if( "Nimbus".equals( info.getName() ) ) {
   UIManager.setLookAndFeel(info.getClassName());
   UIManager.getLookAndFeelDefaults().put("Xxx", "Xxx")
   UIManager.getLookAndFeelDefaults().put("Xxx", "Xxx")
   break;
}

2.字体和部分Colors存储为所有Swings L& F的XxxUIResources,并且需要的XxxUIResources另一个黑客

3.最好使用 UIManager默认值 @camickr获取安装方法的列表,而不是搜索 NimbusDefaults

4.关于Painter ,您可以进行设置自己的Painter(取决于UIDefaults中类型或值)或重写XxxUIResources(有时取决于类型,有时不起作用,因为Nimbus的开发在四分之二的地方就此终止了),

编辑

5.没有什么比这更好的了,我认为@aephyr参与了或ei ???

I'm using the Nimbus Look & Feel within my Java Swing application. The L&F looks great, but i need to change some settings (Fonts, Colors, ... ) to fit the corporate identity of my firm.

The following code sets the L&F of the whole application:

try {
    for( LookAndFeelInfo info : UIManager.getInstalledLookAndFeels() ) {
        if( "Nimbus".equals( info.getName() ) ) {
            UIManager.setLookAndFeel(info.getClassName());
            customizeNimbusLaF();
            break;
        }
    }
}
catch( Exception e ) {
    LogUtility.warning( "cannot set application look and feel" );
    LogUtility.warning( e.getMessage() );
}


The code does, what it is supposed to do (setting the Nimbus Look & Feel). The problem is, that the customizeNimbusLaF() does not work, as i expect it to do.

private final void customizeNimbusLaF() {       
    UIManager.put( "control" , UIConstants.GREY_LIGHT );
    UIManager.put( "nimbusAlertYellow" , UIConstants.YELLOW );
    UIManager.put( "nimbusBase" , UIConstants.GREY_DARK );
    UIManager.put( "nimbusDisabledText" , UIConstants.GREY_DARK );
    UIManager.put( "nimbusFocus" , UIConstants.BLUE_LIGHT );
    UIManager.put( "nimbusGreen" , UIConstants.GREEN );
    UIManager.put( "nimbusInfoBlue" , UIConstants.BLUE_MIDDLE );
    UIManager.put( "nimbusRed", UIConstants.RED );
    UIManager.put( "nimbusSelectionBackground",
    UIConstants.BLUE_MIDDLE );

    UIManager.put( "background" ,UIConstants.GREY_LIGHT );
    UIManager.put( "controlDkShadow" , UIConstants.GREY_DARK );
    UIManager.put( "controlShadow", UIConstants.GREY_MIDDLE );
    UIManager.put( "desktop", UIConstants.BLUE_MIDDLE );
    UIManager.put( "menu", UIConstants.GREY_LIGHT );
    UIManager.put( "nimbusBorder", UIConstants.GREY_MIDDLE );
    UIManager.put( "nimbusSelection", UIConstants.BLUE_MIDDLE );
    UIManager.put( "textBackground", UIConstants.BLUE_LIGHT );
    UIManager.put( "textHighlight", UIConstants.BLUE_LIGHT );
    UIManager.put( "textInactiveText", UIConstants.GREY_MIDDLE );

    // panel
    UIManager.put( "Panel.background", UIConstants.GREY_LIGHT );
    UIManager.put( "Panel.disabled", UIConstants.GREY_LIGHT );
    UIManager.put( "Panel.font", UIConstants.DEFAULT_FONT );
    UIManager.put( "Panel.opaque", true );

    // button
    UIManager.put( "Button.background", UIConstants.GREY_LIGHT );
    UIManager.put( "Button.disabled", UIConstants.GREY_LIGHT );
    UIManager.put( "Button.disabledText", UIConstants.BLUE_MIDDLE );
    UIManager.put( "Button.font", UIConstants.DEFAULT_FONT );

    // menu
    UIManager.put( "Menu.background", UIConstants.GREY_LIGHT );
    UIManager.put( "Menu.disabled", UIConstants.GREY_LIGHT );
    UIManager.put( "Menu.disabledText", UIConstants.GREY_DARK );
    UIManager.put( "Menu.font", UIConstants.MENU_FONT );
    UIManager.put( "Menu.foreground", UIConstants.BLACK );
    UIManager.put( "Menu[Disabled].textForeground",
            UIConstants.GREY_MIDDLE );
    UIManager.put( "Menu[Enabled].textForeground", UIConstants.BLACK );
    UIManager.put( "MenuBar.background", UIConstants.GREY_LIGHT );
    UIManager.put( "MenuBar.disabled", UIConstants.GREY_LIGHT );
    UIManager.put( "MenuBar.font", UIConstants.MENU_FONT );
    UIManager.put( "MenuBar:Menu[Disabled].textForeground",
            UIConstants.GREY_MIDDLE );
    UIManager.put( "MenuBar:Menu[Enabled].textForeground",
            UIConstants.BLACK );
    UIManager.put( "MenuItem.background", UIConstants.GREY_LIGHT );
    UIManager.put( "MenuItem.disabled", UIConstants.GREY_LIGHT );
    UIManager.put( "MenuItem.disabledText", UIConstants.GREY_MIDDLE );
    UIManager.put( "MenuItem.font", UIConstants.MENU_FONT );
    UIManager.put( "MenuItem.foreground", UIConstants.BLACK );
    UIManager.put( "MenuItem[Disabled].textForeground",
            UIConstants.GREY_MIDDLE );
    UIManager.put( "MenuItem[Enabled].textForeground",
            UIConstants.BLACK );

    // tree
    UIManager.put( "Tree.background", UIConstants.BLACK );      
}


The datatypes of the constants in UIConstants are either of type Color of Font depending of the attribute to be set.

The problem is, that only a few look & feel options change. Options like the background of the tree do not change according to the options on the look & feel. I am aware of this, that a few things only change, if I change the painter of a component. But e.g. JPanel does not have such a painter property, but makes problems too.

I have a second question concerning the look & feel: Aren't the painters of e.g. the menuBar supposed to use the colors, which are set in the primary color section, or do I need to implement my own painters for this use?

Can someone tell me where my problem is?

解决方案

1.set for changes is little bit hacky, have to set L&F, then after is possible to make any changes to instaled L&F

pseudocode

if( "Nimbus".equals( info.getName() ) ) {
   UIManager.setLookAndFeel(info.getClassName());
   UIManager.getLookAndFeelDefaults().put("Xxx", "Xxx")
   UIManager.getLookAndFeelDefaults().put("Xxx", "Xxx")
   break;
}

2.Font and part of Colors is stored as XxxUIResources for all Swings L&Fs, and XxxUIResources required another hack,

3.better could be to use UIManager Defaults by @camickr for list of instaled methods, rather than searching into NimbusDefaults

4.about Painter, you can to set own Painter (depends of type or value in UIDefaults) or to override XxxUIResources (depends of type, sometimes, somewere doesn't works, because development of Nimbus ended somwhere in 2nd. of quaters),

EDIT

5.Nothing better around, I think that @aephyr participated of Nimbus development or e.i. ???

这篇关于设置外观颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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