1

標準の JOptionPane 情報アイコンの代わりにカスタム アイコンをインストールしたいと考えています。

私は試した

ImageIcon myCustomIcon = ...;
UIManager.put("OptionPane.informationIcon", myCustomIcon);

ただし、次の行により、明らかにこれは効果がありません。

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

この行をコメントアウトすると、正しい動作が得られます。もちろん、ルック アンド フィールが設定される前と後の両方で、アイコンを UIManager に配置しようとしました。この 2 つを組み合わせて、情報アイコンをグローバルに上書きすることはできますか?

私は現在Ubuntu 10.04に取り組んでいます..

よろしく、モートン

4

2 に答える 2

1

次のような方法で、ルックアンドフィールを設定した後にアイコンを指定しようとしましたか?

JOptionPane.showMessageDialog(frame,
    "Eggs are not supposed to be green.",
    "Inane custom dialog",
    JOptionPane.INFORMATION_MESSAGE,
    myCustomIcon);

アップデート:

次のコードは、Windows 7 で正常に動作します。

import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OptionPaneIcon {
    public static void main (String[] args) {
        ImageIcon myCustomIcon = loadImageIcon("image.png");
        UIManager.put("OptionPane.informationIcon", myCustomIcon);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (UnsupportedLookAndFeelException ex) {
        }
        JOptionPane.showMessageDialog(null, "Hello!");
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    private static ImageIcon loadImageIcon(String path) {
        URL imgURL = OptionPaneIcon.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
                return null;
        }
    }
}

PS せっかちですみません。

于 2011-06-11T14:47:04.087 に答える
1

Metal と Windows の LAF を使用すると問題なく動作します。

LAF が UIManager プロパティをサポートしていない可能性があります。プロパティのリストについては、UIManager のデフォルトを確認してください。

さらにヘルプが必要な場合は、問題を示すSSCCEを投稿してください。

于 2011-06-11T15:24:29.030 に答える