使用javax.swing.ImageIcon显示保存在目录中的jpg i

使用javax.swing.ImageIcon显示保存在目录中的jpg i,java,swing,compiler-errors,imageicon,non-static,Java,Swing,Compiler Errors,Imageicon,Non Static,所以我试着研究这个。我尝试使用从其他地方获得的魔法8球代码,但当J面板弹出询问问题时,我想使用我自己的图像: import java.security.SecureRandom; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class Magic8Ball { private final static ImageIcon image = new Ima

所以我试着研究这个。我尝试使用从其他地方获得的魔法8球代码,但当J面板弹出询问问题时,我想使用我自己的图像:

    import java.security.SecureRandom;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;

    public class Magic8Ball {
    private final static ImageIcon image = new 
    ImageIcon(this.getClass().getResource("BuckminsterFuller.jpg"));

    private final static SecureRandom randomNumber = new SecureRandom();
    private final static String answers[] = {
            "It is certain",
            "It is decidedly so",
            "Without a doubt",
            "Yes - definitely",
            "You may rely on it",
            "As I see it, yes",
            "Most likely",
            "Outlook good",
            "Signs point to yes",
            "Yes",
            "Reply hazy, try again",
            "Ask again later",
            "Better not tell you now",
            "Cannot predict now",
            "Concentrate and ask again",
            "Don't count on it",
            "My reply is no",
            "My sources say no",
            "Outlook not so good",
            "Very doubtful" };


    public static void main(String[] args) {

        boolean askQuestion = true;

        while (askQuestion) {
            String question = getUserQuestion();
            String randomAnswer = getRandomAnswer();

            displayAnswer(question, randomAnswer);

            askQuestion = userWantsToAskAnotherQuestion();
        }

        showExitMessage();
    }

    private static String getUserQuestion() {
        return JOptionPane.showInputDialog(null,
                "PLease enter a yes or no question:",
                "WELCOME: Have your questions answered!",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private static String getRandomAnswer() {
        return answers[randomNumber.nextInt(answers.length)];
    }

    private static void displayAnswer(String question, String randomAnswer) {
        JOptionPane.showMessageDialog(null, question + "\n" + randomAnswer, "The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);
    }

    private static boolean userWantsToAskAnotherQuestion() {
        return 0 == JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?", JOptionPane.YES_NO_OPTION, 0, image);
    }

    private static void showExitMessage() {
        JOptionPane.showMessageDialog(null, "Programmed by my name", "Goodbye! Your answers have been answerd.", JOptionPane.PLAIN_MESSAGE, image);
    }
}
我尝试将图像保存为BuckminsterFuller.jpg,保存在类所在的目录中,保存在名为“images”的单独文件夹中,项目、src、build和类位于该文件夹中

它给了我这个错误:

java.lang.ExceptionInInitializerError Caused by:
java.lang.RuntimeException: Uncompilable source code - non-static
variable this cannot be referenced from a static context    at
Assignment6.Magic8Ball.<clinit>(Magic8Ball.java:10)
java.lang.ExceptionInInitializerError由以下原因引起:
java.lang.RuntimeException:不可编译源代码-非静态
此变量不能从处的静态上下文引用
赋值6.Magic8Ball。(Magic8Ball.java:10)

错误在这一行:

private final static ImageIcon image = new ImageIcon(this.getClass().getResource("BuckminsterFuller.jpg"));
声明静态字段时,不能使用

您可以使用
ClassLoader
class中的方法

像这样:

private final static ImageIcon image = new ImageIcon (ClassLoader.getSystemResource("BuckminsterFuller.jpg"));
编辑

如果在
ImageIcon
构造函数中获得
NullPointerException
,则表示
ClassLoader
未使用正确的图像路径
ClassLoader.getSystemResource()
使用用于启动程序的系统类加载器

例如,如果目录树为:

-> bin
  -> myapp
  -> resources
     -> ...
     -> BuckminsterFuller.jpg
     -> ...
并且您的主类在包myapp中,您应该使用此路径加载图像:

private final static ImageIcon image = new ImageIcon (ClassLoader.getSystemResource("resources/BuckminsterFuller.jpg"));
但这完全取决于应用程序的结构


另外,您正在滥用
静态
修改器,这通常表示设计非常糟糕,请看这个问题:

这与图像或图像图标无关。这是一个应该修复的编译错误。你真的需要回到基础上来,而不是在网上找GUI代码&不懂@AndrewThompson你可能想和4 chan谈谈。谢谢。你可能想把这类问题带到服务台。这是一个技术问答论坛。(不客气。)Hey@Ansharja现在它给了我这个:java.lang.ExceptionInInitializeError引起的:java.lang.NullPointerException位于javax.swing.ImageIcon.(ImageIcon.java:217)位于Assignment6.Magic8Ball.(Magic8Ball.java:10)