2013-04-16 107 views
0

當iv'e試圖打開它時,它不能找到資源。 所以,我已經嘗試過使用that solution,但我得到了另一個問題:exportet jar文件不會訪問資源

Exception in thread "main" java.lang.Error: Unresolved compilation 
problem: Cannot make a static reference to the non-static method 
getClass() from the type Object 
     at Resources.getMainBG(Resources.java:21) 
     at Tetris.<init>(Tetris.java:21) 
     at Main.main(Main.java:5) 

當前的代碼:

import java.awt.*; 
import java.io.File; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 

public class Resources 
{ 
    // this class import the breaks's photos into an image array named "images" 
    private static Image[] images = new Image[7]; 
    private static Image[] BG = new Image[3]; // 1=frame,2=pane,3=nextPane 


    public static Image getImage(int color){ 
     if(images[color]==null){ 
      try{images[color] = new ImageIcon(getClass().getResource("images/block" + color + ".png")).getImage();} 
      catch (Exception e){e.printStackTrace();System.exit(1);}} 
     return images[color];} 

    public static Image getMainBG() { 
     if (BG[0] == null) 
      BG[0] = new ImageIcon(getClass().getResource("images/MainBG.png")).getImage(); 
     return BG[0];} 

    public static Image getPaneBG(){ 
     if (BG[1] == null) 
      BG[1] = new ImageIcon(getClass().getResource("images/PaneBG.png")).getImage(); 
     return BG[1];} 

    public static Image getNextBG() { 
     if (BG[2] == null) 
      BG[2] = new ImageIcon(getClass().getResource("images/NextBG.png")).getImage(); 
     return BG[2];} 
} 

謝是一個很大的幫助!

回答

2

異常說明了這一切 - 代碼在導出時甚至沒有編譯。這種方法是無效的:

public static Image getMainBG() { 
    if (BG[0] == null) 
     BG[0] = new ImageIcon(getClass().getResource("images/MainBG.png")).getImage(); 
    return BG[0]; 
} 

不能調用getClass()不合格一樣,在一個靜態方法。當然,您可以使用Resources.class

請注意,儘管您不應該有任何問題,但您應該在之前檢查您的代碼是否編譯,然後開始打包。

0

試試這個:

Image i = javax.imageio.ImageIO.read(getClass().getResourceAsStream("/images/x.png")); 
0

答案是例外。 你應該叫

Resources.class.getResource(...) 

,而不是

getClass().getResource(...)