0

我试图改变Look&Feel

public class Main {
    public static void main(String[] args) {
        try {
            String osName = System.getProperty("os.name");
            if (osName.contains("Mac")) {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
            }
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e1) {
        }
    }
}

但是在线获取异常UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())

java.lang.NullPointerException
Failed to create resources from application bundle.  Using Java-based resources.
    at java.io.File.<init>(File.java:222)
    at com.apple.resources.LoadNativeBundleAction.run(MacOSXResourceBundle.java:60)
    at com.apple.resources.LoadNativeBundleAction.run(MacOSXResourceBundle.java:33)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.apple.resources.MacOSXResourceBundle.getMacResourceBundle(MacOSXResourceBundle.java:29)
    at com.apple.resources.MacOSXResourceBundle.getMacResourceBundle(MacOSXResourceBundle.java:24)
    at com.apple.laf.AquaLookAndFeel.initResourceBundle(AquaLookAndFeel.java:244)
    at com.apple.laf.AquaLookAndFeel.initComponentDefaults(AquaLookAndFeel.java:260)    
    at com.apple.laf.AquaLookAndFeel.getDefaults(AquaLookAndFeel.java:227)
    at javax.swing.UIManager.setLookAndFeel(UIManager.java:520)
    at javax.swing.UIManager.setLookAndFeel(UIManager.java:564)
    at org.camobap.osx.Main.main(Main.java:26)

有人可以帮我避免这个问题吗?

更新: jdk1.7.0_09

4

1 回答 1

1

看起来您的错误来自您的 setLookAndFeel 行。您应该验证UIManager.getSystemLookAndFeelClassName()是否返回了您的想法。您还可以使用以下代码检查此系统上安装了哪些外观:

    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            System.out.println(info.getName() + " - " + info.getClassName());
            //You can now set the look to the one you want with something like this:
            if ("Mac".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
            }
        }
    } catch (Exception e) {
        // Forget the look and feel!           
    }
于 2012-11-13T18:32:57.547 回答