2

Java Swing アプリケーション内で Nimbus Look & Feel を使用しています。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いずれかの型Colorになります。Font

問題は、わずかなルック アンド フィール オプションしか変更されないことです。ツリーの背景などのオプションは、ルック アンド フィールのオプションによって変わりません。コンポーネントのペインターを変更した場合にのみ、いくつかのことが変わることを認識しています。しかし、たとえば JPanel にはそのようなペインタ プロパティはありませんが、問題もあります。

ルック アンド フィールに関する 2 番目の質問があります。たとえば、メニュー バーのペインターは、原色セクションに設定されている色を使用する必要があるのでしょうか。それとも、この使用のために独自のペインターを実装する必要がありますか?

誰かが私の問題がどこにあるのか教えてもらえますか?

4

2 に答える 2

3

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.フォントと色の一部は、すべての Swings L&F の XxxUIResources として保存され、XxxUIResources には別のハックが必要でした。

3. NimbusDefaultsを検索するよりも、@camickr によるUIManager Defaultsを使用して、インストールされたメソッドのリストを使用する方がよい場合があります。

4. Painter については、独自の Painter を設定するか (UIDefaults のタイプまたは値に依存)、または XxxUIResources をオーバーライドすることができます (タイプに依存します。Nimbus の開発が第 2 四半期のどこかで終了したため、動作しない場合もあります)。

編集

5.周りに何も良い@aephyrことはありません、またはeiが参加したと思いますNimbus development ???

于 2012-09-12T06:31:20.943 に答える
1

私は大きな問題を発見しました。SwingUtilities.updateComponentTreeUI( window )すべてのルック アンド フィール プロパティを UIDefaults に入れた後、呼び出しませんでした。

これで、意図したとおりに実行されます。

別の質問: アプリケーション全体の優先フォントを指定する方法はありますか?

于 2012-09-12T10:46:17.953 に答える