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

标签 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 ,保存在类所在的目录中,即项目、src、构建和类所在的名为“images”的单独文件夹中。

它给了我这个错误:

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)

最佳答案

错误在这一行:

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

声明静态字段时不能使用 this

您可以使用getSystemResource(String name)来自 ClassLoader 类的方法。

像这样:

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

编辑

如果在 ImageIcon 构造函数中收到 NullPointerException,则意味着 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?

关于java - 使用javax.swing.ImageIcon显示我保存在目录中的jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46960004/

相关文章:

C++ 多个定义,即使只给出一个定义

java - 我在 HashMap 中有 4 个项目并希望将它们存储在数组中

java - 自动连线环境为空

java - Mac OS X 10.10 (Yosemite) Java Swing 外观和感觉?

java - 具有相同代码的不同窗口?

java - 设置和删除 JFrame.setContent

java - 主管策略 : Compile error - Resume/Restart/Stop/Escalate incompatible with Directive

c++ - C2975 'N' 的无效模板参数,预期的编译时常量表达式

java - spring mvc 是否有 response.write 可以直接输出到浏览器?

java - Android,覆盖ListView中的项目 View 以显示与实际值略有不同