[英]setImageIcon doesn't set JFrame icon on mac swing window
我已經嘗試過從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);
}
編輯我正在運行這樣的程序,在這里我嘗試在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我已經嘗試了BufferedImage
, ImageIcon
,使用File
..,而我正在使用Mac
如該答案所示,Mac不支持框架圖標。
問題是,你的類似乎是從擴展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);
}
使用此更改Mac中的Dock映像 :
File imageFile = new File("Your image Path");
Image image = ImageIO.read(imageFile);
Application.getApplication().setDockIconImage(image);
對於Windows,請使用以下命令:
YourFrameObject.setIconImage(image);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.