出於某種原因,以下代碼不能正確導入圖像並顯示它。如果我將它的繪製部分註釋掉,它就會工作。我無法弄清楚爲什麼。Eclipse,將不會導入圖像?
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class BoardBuild extends JPanel{
String picPath = "pictures/";
String[] fileName = {"board.png"};
ClassLoader cl = BoardBuild.class.getClassLoader();
URL imgURL[] = new URL[1];
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG;
public BoardBuild() throws Exception {
for (int x = 0; x < 1; x++)
imgURL[x] = cl.getResource(picPath + fileName[x]);
imgBG = tk.createImage(imgURL[0]);
}
public void paintComponent(Graphics g) {
g.drawImage(imgBG, 0, 0, 150,150 , 0, 0,73, 73, this);
}
}
由於它是Eclipse的,拍出來的照片的文件夾workspace\BoardMaskTest\src\pictures
英寸 我將不勝感激。