我正在嘗試更改 Java 應用程式的圖示,但似乎沒有任何效果。
我正在嘗試使用以下方法從資源路徑中獲取影像:
getClass().getResource("/AppIcon.png")
有時我會收到諸如找不到 URL 之類的錯誤。
uj5u.com熱心網友回復:
用于表單圖示的影像可以是任何影像,但必須作為Image型別(不是ImageIcon型別)加載。JFrame#setIconImage() 方法將自動調整加載影像的大小。這里只是幾種方法。這些示例假定代碼駐留在擴展 JFrame的類中:
示例 #1:
try {
/* For a Form's title bar icon....
Don't use this for animated .gif images otherwise your GUI will
freeze. I'm not exactly sure why but it seems as though the gif
frames continuously cycle as though in an infinite loop. If you
want to use an animated .gif image as a Form's title bar icon
then don't use Toolkit, use ImageIO.read() instead since it will
only utilize the first gif frame as the image. */
// Be sure to set the path string to your specific resources directory.
this.setIconImage(java.awt.Toolkit.getDefaultToolkit()
.getImage(getClass().getResource("/resources/images/Apple/png").getFile()));
}
catch (java.lang.NullPointerException ex) {
// Do what you want with caught exception.
ex.printStackTrace();
}
示例 #2:
try {
/* Can be used to also display an animated gif for the Form's Title
bar icon but only the first gif frame is utilized. */
// Be sure to set the path string to your specific resources directory.
File pathToFile = new File(getClass().getResource("/resources/images/Apple.png").getFile());
Image image = ImageIO.read(pathToFile);
this.setIconImage(image);
}
catch (IOException ex) {
// Do what you want with caught exception.
ex.printStackTrace();
}
更新:
正如@AndrewThompson 在評論中所說,上述兩個示例在 JAR 檔案中無法按預期作業。如果通過 IDE 運行它們將起作用,但實際上除了測驗之外沒有什么好處。要在分布式 JAR 檔案中使用上面的兩個示例,請參閱示例 #3和示例 #4:
示例#3:
try {
/* For a Form's title bar icon.... To be used in a distributive JAR.
Don't use this for animated .gif images otherwise your GUI will
freeze. I'm not exactly sure why but it seems as though the gif
frames continuously cycle as though in an infinite loop. If you
want to use an animated .gif image as a Form's title bar icon
then don't use Toolkit, use ImageIO.read() instead since it will
only utilize the first gif frame as the image. */
// Be sure to set the path string to your specific resources directory.
java.net.URL url = getClass().getResource("/resources/images/Apple.png");
this.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(url));
}
catch (java.lang.NullPointerException ex) {
// Do what you want with caught exception.
ex.printStackTrace();
}
示例 #4:
try {
/* For a Form's title bar icon.... To be used in a distributive JAR.
Can be used to also display an animated gif for the Form's Title
bar icon but only the first gif frame is utilized. */
// Be sure to set the path string to your specific resources directory.
java.net.URL url = getClass().getResource("/resources/images/Apple.png");
Image image = ImageIO.read(url);
this.setIconImage(image);
}
catch (IOException ex) {
// Do what you want with caught exception.
ex.printStackTrace();
}
甚至不必理會示例 #1 和 #2,它們現在只是留在這里供參考。只需使用示例#3 或示例#4。兩者都可以在 IDE 或分布式 JAR 檔案中作業。
uj5u.com熱心網友回復:
解決方案; 一切都適用于 Windows,但我使用的是 Mac :D 所以我開始查看帶有 awt 包的 Taskbar 類并找到了解決方案(感謝flohall)
try {
var image = new ImageIcon(Objects.requireNonNull(Main.class.getResource("/AppIcon.png")));
frame.setIconImage(image.getImage());
if (System.getProperty("os.name").startsWith("Mac") || System.getProperty("os.name").startsWith("Darwin")) {
Taskbar taskbar = Taskbar.getTaskbar();
try {
taskbar.setIconImage(image.getImage());
} catch (final UnsupportedOperationException e) {
System.out.println("Can't set taskbar icon.");
} catch (final SecurityException e) {
System.out.println("Warning. Can't set taskbar icon due to security exceptions.");
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
所以我們告訴任務欄使用內置的 awt Taskbar 類來改變它的圖示taskbar.setIconImage(image.getImage());。這解決了我需要的大部分事情。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/481637.html
上一篇:將影像放在pygame網格上
下一篇:djangorest上傳多張圖片
