2012-10-22 35 views
0

我想要的圖標添加到一個JFrame標題欄在OS X我用的代碼是:的Java:JFrame的圖標,標題欄不顯示(OS X)

ImageIcon icon = new ImageIcon("src/images/theIcon.gif"); 
System.out.println("Icon dimensions are " + icon.getIconWidth() + " by " + icon.getIconHeight()); 
frame.setIconImage(icon.getImage()); 

無圖標顯示出來,並控制檯輸出顯示圖像的正確尺寸。 表達式setIconImage(icon.getImage())肯定有問題,但我看不到。

我在Mac OS 10.8.2上運行Java SE 6和Eclipse Juno。其實,我想知道這是否是OS X的問題。

回答

0

默認情況下,OS X不會使用圖標繪製標題欄。您可以啓用跨平臺的外觀來繪製圖標。但是,這樣做可能會傷害你的眼睛,因爲它不是特別漂亮。

在創建JFrame之前執行此操作(例如,在main方法中)。

try { 
    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
    JFrame.setDefaultLookAndFeelDecorated(true); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

下面是截圖使用和不使用上面的代碼:

JFrame with and without cross platform l&f