ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 设置外观颜色

2019-10-07 03:00:18  阅读:289  来源: 互联网

标签:uimanager look-and-feel java swing nimbus


我正在使用Nimbus Look&感觉我的Java Swing应用程序.
L& F看起来很棒,但我需要更改一些设置(字体,颜色……)以适应我公司的企业形象.

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

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() );
}

代码确实如此(设置Nimbus Look& Feel).
问题是,customizeNimbusLaF()不起作用,正如我所期望的那样.

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 );      
}

UIConstants中常量的数据类型是Color of Font的类型,具体取决于要设置的属性.

问题是,只有少数看起来和感觉选择改变.树的背景等选项不会根据外观和选项上的选项而改变.感觉.我知道这一点,如果我改变组件的画家,一些事情只会改变.但是例如JPanel没有这样的画家属性,但也有问题.

关于外观和问题,我有第二个问题.感觉:
不是例如画家menuBar应该使用在主色部分设置的颜色,还是我需要实现自己的画家才能使用?

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

解决方法:

1.设置变化有点hacky,必须设置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.Font和部分颜色存储为XxxUIResources用于所有Swings L& Fs和XxxUIResources required another hack,

3.better可以使用@camickr的UIManager Defaults作为instaled方法的列表,而不是搜索到NimbusDefaults

4.about Painter,您可以设置自己的Painter(取决于UIDefaults中的类型或值)或覆盖XxxUIResources(取决于类型,有时,有些不起作用,因为Nimbus的开发在第二季度结束了somwhere),

编辑

5.Nothing better around,我认为@aephyr参与了Nimbus开发或e.i. ???

标签:uimanager,look-and-feel,java,swing,nimbus
来源: https://codeday.me/bug/20191007/1864141.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有