2015-04-05 68 views
-1

我在java中創建了一個貪吃蛇遊戲,一切似乎都正常。但是當我試圖在JButton中添加蛇頭圖像時,圖像不會顯示。JButton中的ImageIcon

ImageIcon img = new ImageIcon("shead.jpg"); 
    for (int i = 0; i < 3; i++) 
    { 
     if (i == 0){ 
      lb[i] = new JButton("lb" + i, img); 
     } 
     else { 
      lb[i] = new JButton("lb" + i); 
     } 
     lb[i].setEnabled(false); 
     p1.add(lb[i]); 
     lb[i].setBounds(lbx[i], lby[i], 10, 10); 
     lbx[i + 1] = lbx[i] - 10; 
     lby[i + 1] = lby[i]; 
    } 
+1

你確定你的圖標可以在磁盤上找到?可能是JVM正在尋找錯誤路徑中的文件。看到這裏例如:http://stackoverflow.com/questions/16631636/what-is-the-correct-path-to-display-an-imageicon-png-file-for-windows-7 – GhostCat 2015-04-05 08:45:33

+0

你的jpg文件在哪裏在根文件夾中? – 2015-04-05 08:46:17

+0

類文件和圖像都在同一個文件夾中 – Boopesh 2015-04-05 08:48:23

回答

-1

設置圖像的整個路徑「C:\ User \ Folder \ Folder \ file ...」。

此外,我認爲它需要一個PNG文件才能工作。它不能是JPG。