[英]Nimbus Look&Feel refresh Painter
我在我的 Swing 应用程序中使用了 Nimbus 的外观和感觉。
我设置了外观的UIDefaults
的主要和次要属性。 颜色是对的。 现在我遇到了问题,组件的画家使用了在更新颜色主题之前定义的颜色。
有没有办法更新所有组件的画家以使用新颜色,或者我是否需要为每个属性实现自定义画家?
在UIDefaults
设置属性后,我已经调用了SwingUtilities.updateComponentTreeUI(window)
。
编辑:
以下代码设置整个应用程序的 L&F:
try { for( LookAndFeelInfo info : UIManager.getInstalledLookAndFeels() ) { if( "Nimbus".equals( info.getName() ) ) { UIManager.setLookAndFeel(info.getClassName()); customizeNimbusLaF(); SwingUtilities.updateComponentTreeUI( appWindow ); break; } } } catch( Exception e ) { LogUtility.warning( "cannot set application look and feel" ); LogUtility.warning( e.getMessage() ); }
代码做了什么,它应该做什么(设置 Nimbus 外观和感觉)。 问题是,菜单的Painters
和其他组件使用旧颜色。
以下代码设置颜色:
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
类型,具体取决于要设置的属性。
有人能告诉我我的问题出在哪里吗?
问候迈克尔
将这些位置更改为代码行
UIManager.setLookAndFeel(info.getClassName());
customizeNimbusLaF();
到
customizeNimbusLaF();
UIManager.setLookAndFeel(info.getClassName());
我想为这里提出的这种方法添加一个替代方法。
尤其是下期描述的UIManager相关的所有问题,都由material-ui-swing库以Material Theming System的概念解决。
材料主题系统给予设置你的颜色和风格决定像的可能性/无边框,带圆角或不被记录的单一类的边界在这里包下mdlaf.themes
。
使用示例可以是以下一个
try {
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(false);
MaterialTheme theme = new MaterialLiteTheme();
// here your custom config theme.setXXX();
MaterialLookAndFeel material = new MaterialLookAndFeel(theme);
UIManager.setLookAndFeel(material);
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
您也可以保留主题,进行自定义更改,并在阅读时调用SwingUtilities.updateComponentTreeUI(window)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.