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

时间:2017-10-26 17:05:18

标签: 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 
    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,构建和类所在的位置。

它给了我这个错误:

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)

1 个答案:

答案 0 :(得分:0)

错误在这一行:

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

声明静态字段时,无法使用this

您可以使用ClassLoader课程中的getSystemResource(String name)方法。

像这样:

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

修改

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

例如,如果您的目录树是:

-> bin
  -> myapp
  -> resources
     -> ...
     -> BuckminsterFuller.jpg
     -> ...

您的Main类在包myapp中,您应该使用此路径加载图像:

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

但这完全取决于您的应用程序的结构。

另外,你滥用static修饰符,这通常表示设计非常糟糕,请看一下这个问题:Why are static variables considered evil?