我試過這種方式,但它沒有改變?如何設置圖標爲JFrame
ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico");
frame.setIconImage(icon.getImage());
我試過這種方式,但它沒有改變?如何設置圖標爲JFrame
ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico");
frame.setIconImage(icon.getImage());
更好地使用.png文件; .ico是Windows特定的。最好不要使用文件,而要使用類資源(可以打包在應用程序的jar中)。
URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());
儘管您甚至可能會考慮使用setIconImages作爲幾種尺寸的圖標。
即使圖像路徑是正確的,我也會收到空指針異常 – vijay 2013-03-27 12:48:05
延遲響應:圖標應與您調用getClass()的類位於同一個jar中,並且路徑使用'/' 。 – 2013-04-16 13:09:59
嘗試將您的圖像放在src文件夾以外的單獨文件夾中。然後,使用ImageIO加載您的圖像。它應該看起來像這樣:
frame.setIconImage(ImageIO.read(new File("res/icon.png")));
上面的答案不適用於我,但這是有效的! – trinity420 2016-07-05 19:25:03
最後,我發現設置jframe圖標的主要問題。這是我的代碼。它與其他代碼類似,但這裏有幾件事要介意這個遊戲。
this.setIconImage(new ImageIcon(getClass().getResource("Icon.png")).getImage());
1)把在JFrame中的windowOpened事件
2)這個代碼將圖片在主文件夾,其中所有的形式和java文件被創建例如
src\ myproject\ myFrame.form
src\ myproject\ myFrame.java
src\ myproject\ OtherFrame.form
src\ myproject\ OtherFrame.java
src\ myproject\ Icon.png
3),最重要的是,文件的名稱是區分大小寫是的icon.png不會工作,但的icon.png。
這樣,即使在最終構建您的項目之後,您的圖標仍將存在。
frame.setIconImage(new ImageIcon(URL).getImage());
/* 框架的JFrame 的setIcon方法,在你的框架 新的ImageIcon設置一個新的圖標使類的一個新實例的(所以你可以從你給的網址一個新的圖標) 最後的getImage返回你需要 這是一個「快」的方式,使一個圖標,該圖標對我來說是有幫助的,因爲它的代碼 */
請爲您的代碼添加一些說明,具體說明它的作用,以及它如何解決問題。 – 2014-04-21 20:37:30
勇可以試試下面的方法,
myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
一行
這裏是我用來設置的JFrame
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
try{
setIconImage(ImageIO.read(new File("res/images/icons/appIcon_Black.png")));
}
catch (IOException e){
e.printStackTrace();
}
圖標在你的代碼的這幾行代碼只需複製和替換「imgUrl的」用圖像的代碼(你想設置爲jframe
圖標)的位置。
JFrame.setDefaultLookAndFeelDecorated(true);
//Create the frame.
JFrame frame = new JFrame("A window");
//Set the frame icon to an image loaded from a file.
frame.setIconImage(new ImageIcon(imgURL).getImage());
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Scanner;
public class WindowUtilities
{
public static void setIconImage(Window window)
{
window.setIconImage(Toolkit.getDefaultToolkit().getImage(WindowUtilities.class.getResource("/Icon.jpg")));
}
public static String resourceToString(String filePath) throws IOException, URISyntaxException
{
InputStream inputStream = WindowUtilities.class.getClassLoader().getResourceAsStream(filePath);
return toString(inputStream);
}
// http://stackoverflow.com/a/5445161/3764804
private static String toString(InputStream inputStream)
{
try (Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A"))
{
return scanner.hasNext() ? scanner.next() : "";
}
}
}
所以使用這個變得簡單調用
WindowUtilities.setIconImage(this);
內
你的某處課程延伸JFrame
。
例如,當使用Maven
時,Icon.jpg
必須位於myproject\src\main\resources
中。
public FaceDetection() {
initComponents();
//Adding Frame Icon
try {
this.setIconImage(ImageIO.read(new File("WASP.png")));
} catch (IOException ex) {
Logger.getLogger(FaceDetection.class.getName()).log(Level.SEVERE, null, ex);
}
}'
這對我有用。
這適用於我。
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(".\\res\\icon.png"));
對於導出jar文件,您需要配置構建路徑以包含res文件夾並使用以下代碼。
URL url = Main.class.getResource("/icon.png");
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
在文件路徑中似乎存在一些問題。如果未在指定路徑中找到圖像,將設置默認圖像圖標。 – prasanth 2013-03-27 11:24:11