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

Using javax.swing.ImageIcon to display a jpg i saved in the directory

所以我试着研究这个。我尝试使用从其他地方获得的这个神奇的 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 在 class 所在的目录中,在一个名为 "images" 的单独文件夹中,其中项目、src、构建和 class 在.

它给我这个错误:

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

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

像这样:

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

编辑

如果在 ImageIcon 构造函数中得到 NullPointerException,这意味着 ClassLoader 没有使用正确的图像路径。 ClassLoader.getSystemResource () 使用系统 classloader,用于启动程序的那个。

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

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

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

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

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

另外,你滥用了static修饰符,这通常表示设计很差,看看这个问题:Why are static variables considered evil?