所以我的应用程序中有一个弹出对话框,告诉用户该程序。一切都很顺利,直到自定义图标。这就是我的尝试:
尝试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:
使用网址
所有对程序没有任何影响,而不是图像,我什么都没得到。
详细信息:
救救我! :(
答案 0 :(得分:9)
这对我有用:
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
final ImageIcon icon = new ImageIcon("C:\\Users\\John\\Desktop\\lol.jpg");
JOptionPane.showMessageDialog(null, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
}
}
以下是使用网址的变体:
import javax.swing.*;
import java.net.*;
public class TestIcon
{
public static void main(String[] args) throws Exception
{
final ImageIcon icon = new ImageIcon(new URL("http://www.gravatar.com/avatar/a1ab0af4997654345d7a949877f8037e?s=128&d=identicon&r=PG"));
JOptionPane.showMessageDialog(null, "Blah blah blah", "About", JOptionPane.INFORMATION_MESSAGE, icon);
}
}
答案 1 :(得分:3)
我知道这有点旧,但由于没有回复解决了我的问题,经过一些研究后,这对我有用(使用java 1.7):
我使用了getClass().getResource(<path>)
这样的方法:
ImageIcon icon = new ImageIcon(getClass().getResource(<pathToIcon>));
在我看来,在您的项目中创建“资源”文件夹是一种很好的做法,在其中包含“图标”文件夹,并且每当您需要图标(或其他任何内容,例如音频文件,图像等)
答案 2 :(得分:0)
答案 3 :(得分:0)
试试这个:
JPanel panel = new JPanel();
BufferedImage myPicture = null;
try
{
myPicture = ImageIO.read(new File("home/user/Pictures/default.jpg"));
}
catch(Exception ex){}
panel.add(new JLabel(new ImageIcon(myPicture)));
panel.add(new JLabel("blah blah blah"));
Object[] options = {};
JOptionPane pane = new JOptionPane();
pane.showOptionDialog(null, panel, "About", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, null);
答案 4 :(得分:0)
同样的事情发生在我身上,感谢上帝,我看到我的图片不是加载在'源'文件中,而是在'bin'文件中..路径错误
ImageIcon preg1 = new ImageIcon("C:\\Java\\TestPsicologico\\bin\\Preg1.jpg");
答案 5 :(得分:0)
每个人都是对的,只是复制的路径不正确。
您只需将首选图片放在 项目文件夹 中 并且您的图像将显示在项目导航选项卡中,之后只需复制图像路径并将其粘贴到:
final ImageIcon icon = new ImageIcon("*Paste copied path*");
JOptionPane.showMessageDialog(null, infoMessage, " " + titleBar, JOptionPane.INFORMATION_MESSAGE,icon);