简体   繁体   English

exportet jar文件不会访问资源

[英]an exportet jar file won't access resources

When iv'e tried to open it it could'nt find the resources. 当iv'e试图打开它时,它无法找到资源。 So, I've already tried to use that solution but i get another problem: 所以,我已经尝试使用该解决方案,但我得到另一个问题:

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)

The current code: 目前的代码:

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

thank's a lot for your help! 非常感谢你的帮助!

The exception says it all - the code didn't even compile when you exported it. 例外情况说明了 - 导出它时代码甚至没有编译。 This method is invalid: 此方法无效:

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

You can't call getClass() unqualified like that in a static method. 你不能像静态方法那样调用getClass()不合格。 You could use Resources.class instead, of course. 当然,您可以使用Resources.class

Note that you shouldn't have got anywhere near as far as an exception though: you should check that your code compiles before you start packaging it. 请注意,除了异常之外,您不应该接近任何地方:您应该开始打包之前检查您的代码是否已编译。

尝试这个:

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

The answer is in the Exception. 答案在异常中。 You should call 你应该打电话

Resources.class.getResource(...)

instead of 代替

getClass().getResource(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM