0

iv'e を開こうとしたときに、リソースが見つかりませんでした。だから、私はすでにそのソリューションを使用しようとしましたが、別の問題が発生します:

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];}
}

どうもありがとうございました!

4

3 に答える 3

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もちろん、代わりに使用することもできます。

ただし、例外に近いところに行くべきではないことに注意してください。パッケージ化を開始する前に、コードがコンパイルされることを確認する必要があります。

于 2013-04-16T22:20:07.420 に答える
0

これを試して:

Image i = javax.imageio.ImageIO.read(getClass().getResourceAsStream("/images/x.png"));
于 2013-04-16T22:20:53.240 に答える
0

答えは例外にあります。あなたは電話するべきです

Resources.class.getResource(...)

それ以外の

getClass().getResource(...)
于 2013-04-16T22:21:36.960 に答える