2012-11-13 27 views
0

我試圖改變Look&FeelJava Swing:無法從應用程序包創建資源。使用基於Java的資源

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) 

例外誰能HEPL我避免這個問題?

更新: jdk1.7.0_09

+0

[按照蘋果(http://developer.apple.com/library/mac/#documentation/Java/參考/ Java_PropertiesRef/Articles/JavaSystemProperties.html):*當在你的應用程序中設置一個屬性時(使用System.setProperty),確保它是你的main方法中的第一個語句之一。這樣做會在AWT加載之前設置屬性,以確保其生效。*也許嘗試在不檢查操作系統的情況下設置該屬性並查看它是否可用 –

+0

哪種語言在Mac上的語言和文本系統首選項的頂部位置?這似乎是你有一些問題與MAC資源包 –

+0

運行良好在我的Mac與JDK1.7 – Robin

回答

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!   
    }