繁体   English   中英

设置外观颜色

[英]Set look and feel color

我在Java Swing应用程序中使用Nimbus外观。 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外观)。 问题是, 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中的常量的数据类型取决于要设置的属性,其类型为“ Font Color ”。

问题是,只有少数外观选项改变。 诸如树的背景之类的选项不会根据外观上的选项而改变。 我知道这一点,只有当我更改组件的绘制程序时,某些事情才会更改。 但是,例如,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需要另一个hack

3.更好的办法是使用@camickr的UIManager Defaults作为安装方法的列表,而不是搜索NimbusDefaults

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

编辑

5. 周围没有更好的事情 ,我认为@aephyr参与了Nimbus development或ei ???

我发现了主要问题。 将所有外观属性放入UIDefaults之后,我没有调用SwingUtilities.updateComponentTreeUI( window )

现在,它执行了我想要的方式。

另一个问题:是否可以指定整个应用程序的首选字体?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM