0

I am trying to add an icon to a JFrame titlebar in OS X. The code I have used is:

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

No icon shows up, and the console output shows the correct dimensions for the image. There must be something wrong with the expression setIconImage(icon.getImage()), but I can't see what.

I am on Mac OS 10.8.2, running Java SE 6 and Eclipse Juno. Actually, I'm wondering if this is an OS X issue.

4

1 回答 1

1

默认情况下,OS X 不会用图标绘制标题栏。您可以启用跨平台外观来绘制图标。但是,这样做可能会伤害您的眼睛,因为它不是特别漂亮。

在创建 JFrame 之前执行此操作(例如在main方法中)。

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

这是带有和不带有上述代码的屏幕截图:

JFrame 带和不带跨平台 l&f

于 2013-08-07T06:59:46.287 回答