0
所以,当我尝试下面的代码:网址加载错误
//Main Class
package com.mgflow58.Main;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args) {
BufferedImage mainicon = null;
JFrame window = new JFrame("Guppy's Adventure");
window.add(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
try {
String mainurlstring = "icon.gif";
URL mainurl = new URL(mainurlstring);
try {
mainicon = ImageIO.read(mainurl);
window.setIconImage(new ImageIcon(mainicon).getImage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我得到以下错误:
java.net.MalformedURLException: no protocol: icon.gif
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.mgflow58.Main.Game.main(Game.java:28)
文件icon.gif是在根文件夹,我甚至试图将它复制到执行代码的类所在的src/com/mgflow58/Main中。我做错了什么?无论我尝试使用哪种方法,我都无法设置图像图标,而且我一直在努力寻找答案。