Java JOptionPane.showMessageDialog自定义图标问题?
因此,我的应用程序中有一个弹出对话框,告诉用户该程序的信息。一切都很顺利,直到出现自定义图标。这是我尝试过的:
尝试 1:
JOptionPane.showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("home/user/Pictures/default.jpg"));
尝试 2:
final icon = new ImageIcon("home/user/Pictures/default.jpg"));
JOptionPane.showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
尝试 3:
final icon = new ImageIcon("home/user/Pictures/default.jpg"));
showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
尝试 4:
(尖叫java)
尝试 5:
使用 URL 的
All 对程序没有影响,我没有得到任何东西,而不是图像。
详细信息:
- 没有例外
- 文件路径确实存在
- 我的IDE不返回异常,也不返回任何类型的警告
- 是的,我也尝试过路径/home/user/Pictures/default.jpg
- .ico、.png、.jpg 不起作用。不过我现在不太确定 .gif 的情况。
帮我! :(
So I have a pop-up dialog in my application that tells the user about the program. Everything was going fine until the custom icon. Here's what I've attempted:
Attempt 1:
JOptionPane.showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("home/user/Pictures/default.jpg"));
Attempt 2:
final icon = new ImageIcon("home/user/Pictures/default.jpg"));
JOptionPane.showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
Attempt 3:
final icon = new ImageIcon("home/user/Pictures/default.jpg"));
showMessageDialog(dialog, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
Attempt 4:
(Screaming at java)
Attempt 5:
Using URL's
All have had no affect on the program and instead of an image, I don't get anything.
Details:
- No exceptions
- The file path DOES exist
- My IDE doesn't return exceptions, NOR any warnings of any sort
- Yes, I've put also tried the path /home/user/Pictures/default.jpg
- .ico's, .png's, .jpg's don't work. I'm not so sure about .gif's right now though.
Help me! :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这对我有用:
这是一个使用 URL 的变体:
This worked for me:
Here is a variant that uses a URL:
我知道这有点老了,但由于没有回复解决我的问题,经过一番研究,这对我有用(使用 java 1.7):
我使用了
getClass().getResource(
getClass().getResource(
getClass().getResource(< path>)
像这样的方法:在我看来,在项目中创建一个“资源”文件夹,并在其中创建一个“图标”文件夹,并在每次需要时引用该位置是一个很好的做法一个图标(或其他任何内容,例如音频文件、图像等)
I know this is a little old, but as there was no reply that solved my question, after some research this is what worked for me (working with java 1.7):
I have used the
getClass().getResource(<path>)
method like this:It seems to me as a good practice to create a 'resources' folder in your project, and inside it a 'icons' folder, and to refer to that location every-time you need an icon (or anything else such as audio files, images etc)
这可能会有所帮助: http://download.oracle.com/javase/教程/uiswing/components/icon.html
Here this may help: http://download.oracle.com/javase/tutorial/uiswing/components/icon.html
试试这个:
Try this:
同样的事情也发生在我身上,感谢上帝,我发现我的图像没有加载到“源”文件中,而是加载到“bin”文件中..路径错误
The same thing happened to me, thank God I looked that my image was loaded not in the 'source' file, it was in 'bin' file.. the path was wrong
大家都说得对,只是复制的路径不正确。
您只需将首选图像放入您的项目文件夹
您的图像将显示在项目导航选项卡中,之后只需复制图像路径并将其粘贴到:
Everyone was right it's just that the path copied is incorrect.
You just have to place the preferred image in your project's folder
and your image will show up in the project navigation tab , after that just copy the image path and paste it into: