我有一個問題,所以我會在下面顯示它。首先,感謝您的閱讀!在Netbeans編碼一個掃雷遊戲。在代碼中,我必須包括數字,炸彈,標誌的圖像...在Java中包含圖像
首先,我在「src」中創建了一個「images」文件夾。然後我去屬性,並添加資源(它顯示項目中的圖像文件夾)。
我的工作區:https://flic.kr/p/qevcEm
那麼這是很重要的,我搜索谷歌和使用此代碼,但得到了麻煩。
public void imgmine() {
String name;
for (int i = 0; i <= 8; i++) {
name = "images/" + i + ".png";
ic[i] = new ImageIcon(this.getClass().getResource(name));
/// i was edit above line. but 9 10 11 12 13 wasnt.
}
ic[9] = new ImageIcon("bomb.png");
ic[10] = new ImageIcon("flag.png");
ic[11] = new ImageIcon("newgame.png");
ic[12] = new ImageIcon("crape.gif");
ic[13] = new ImageIcon("winner.gif");
}
在windows代碼中沒有錯誤。但是編者說:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at Minesweeper.imgmine(Minesweeper.java:522)
at Minesweeper.<init>(Minesweeper.java:43)
at Main.main(Main.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
522線是
ic[i] = new ImageIcon(this.getClass().getResource(name));
'Minesweeper.class'的FQN是什麼? – PeterMmm 2014-12-02 15:35:19
什麼是FQN先生?我的英文不是很好:( – TruongHieu 2014-12-02 15:40:22
[FQN](http://en.wikipedia.org/wiki/Fully_qualified_name)like'my.package.game.Minesweeper'。 – PeterMmm 2014-12-02 15:41:54