gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:04:56 36 4
gpt4 key购买 nike

所以我尝试研究这个。我尝试使用从其他地方获得的这个神奇的 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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com