我在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没有这样的画笔属性,但也会产生问题。
我还有第二个关于外观的问题:比如menuBar的画师不是应该使用在原色部分设置的颜色吗?或者我需要实现自己的画师来实现这个用途吗?
谁能告诉我我的问题出在哪里?
发布于 2012-09-12 14:31:21
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.对于所有的摇摆L&F和XxxUIResources required another hack,字体和部分颜色被存储为XxxUIResources,
3.更好的做法是通过@camickr使用UIManager Defaults获取已安装方法的列表,而不是搜索NimbusDefaults
4.about Painter,你可以设置自己的Painter (依赖于UIDefaults中的类型或值)或覆盖XxxUIResources (依赖于类型,有时,有些是不起作用的,因为Nimbus的开发在第二个地方结束了。1/4),
编辑
5.Nothing better around,我认为@aephyr参与了Nimbus development或e.i.???
发布于 2012-09-12 18:46:18
我找到了主要的问题。在将所有外观和感觉属性放入UIDefaults之后,我没有调用SwingUtilities.updateComponentTreeUI( window )。
现在它按照我想要的方式运行了。
另一个问题:有没有办法指定整个应用程序的首选字体?
https://stackoverflow.com/questions/12382143
复制相似问题