setImageIcon不在mac swing窗口上设置JFrame图标

时间:2014-04-30 01:25:30

标签: java macos swing jframe imageicon

我已经尝试过Stack的大量代码。出于某种原因,它只是没有为我的JFrame设置ImageIcon,注释是其他尝试没有用;我避免调用super以便我可以引用JFrame - GUIPhotoAlbum extends JFrame;代码:

public GUIPhotoAlbum ()
{
    super("PhotoAlbum");
    ImageIcon img = new ImageIcon("Photos/albumIcon.png");
    this.setIconImage(img.getImage());

    /*
    try{
        setIconImage(ImageIO.read(new File("Photos/albumIcon.png")));
    }catch(Exception e){
        System.out.print("Didn't work.");
    }
    */

    setSize(875, 625);
    this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout(5, 5));

    initComponents();
    initMenuBar();

    initTopPanel();
    add(topPanel, BorderLayout.CENTER);

    initBottomPanel();
    add(bottomPanel, BorderLayout.SOUTH);

    addListeners();

    setLocationRelativeTo(null);
    setVisible(true);
}

EDIT 我正在运行这样的程序,我尝试在GUIPhotoAlbum()构造函数中设置JFrame的ImageIcon;这里是司机:

public class AlbumDriver
{   
    public static void main (String [ ] args)
    {
           SwingUtilities.invokeLater 
           (
                 new Runnable()
                 {
                        @Override
                        public void run()
                        {
                            GUIPhotoAlbum pa = new GUIPhotoAlbum();
                        }   
                 }
           ); 
    }

}

我在这里做错了什么? PS我尝试BufferedImageImageIcon,使用File ..我正在使用Mac

3 个答案:

答案 0 :(得分:4)

Mac不支持框架图标,如this answer中所示。

答案 1 :(得分:2)

问题是,您的课程似乎是从JFrame延伸,但您正在创建JFrame的新实例,而是设置它的图标......

JFrame newFrame = new JFrame("PhotoAlbum");

ImageIcon img = new ImageIcon("Photos/albumIcon.png");
newFrame.setIconImage(img.getImage());

不要创建JFrame的第二个实例,在这种情况下不需要newFrame ...

例如......

public GUIPhotoAlbum ()
{
    super("PhotoAlbum");
    ImageIcon img = new ImageIcon("Photos/albumIcon.png");
    setIconImage(img.getImage());

    /*
       //when uncommented, exception is never thrown
    try{
        setIconImage(ImageIO.read(new File("Photos/albumIcon.png")));
    }catch(Exception e){
        System.out.print("Didn't work.");
    }
    */

    // Hint use pack instead, but only after
    // You've finished adding the components to the frame
    setSize(875, 625);
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout(5, 5));

    initComponents();
    initMenuBar();

    initTopPanel();
    add(topPanel, BorderLayout.CENTER);

    initBottomPanel();
    add(bottomPanel, BorderLayout.SOUTH);

    addListeners();

    setLocationRelativeTo(null);
    setVisible(true);
}

答案 2 :(得分:1)

使用它来更改mac:

中的停靠图像
File imageFile = new File("Your image Path");
Image image =  ImageIO.read(imageFile);
Application.getApplication().setDockIconImage(image);

对于Windows使用此:

YourFrameObject.setIconImage(image);