Java JOptionPane.showMessageDialog自定义图标问题?

发布于 2024-11-28 12:07:05 字数 1085 浏览 1 评论 0原文

因此,我的应用程序中有一个弹出对话框,告诉用户该程序的信息。一切都很顺利,直到出现自定义图标。这是我尝试过的:

尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

疧_╮線 2024-12-05 12:07:06

这对我有用:

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);
    }
}

这是一个使用 URL 的变体:

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);
    }
}

This worked for me:

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);
    }
}

Here is a variant that uses a URL:

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);
    }
}
冷情 2024-12-05 12:07:06

我知道这有点老了,但由于没有回复解决我的问题,经过一番研究,这对我有用(使用 java 1.7):

我使用了 getClass().getResource(getClass().getResource(getClass().getResource(< path>) 像这样的方法:

ImageIcon icon = new ImageIcon(getClass().getResource(<pathToIcon>));

在我看来,在项目中创建一个“资源”文件夹,并在其中创建一个“图标”文件夹,并在每次需要时引用该位置是一个很好的做法一个图标(或其他任何内容,例如音频文件、图像等)

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:

ImageIcon icon = new ImageIcon(getClass().getResource(<pathToIcon>));

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)

已下线请稍等 2024-12-05 12:07:06

试试这个:

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);

Try this:

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);
纵情客 2024-12-05 12:07:06

同样的事情也发生在我身上,感谢上帝,我发现我的图像没有加载到“源”文件中,而是加载到“bin”文件中..路径错误

ImageIcon preg1 = new ImageIcon("C:\\Java\\TestPsicologico\\bin\\Preg1.jpg"); 

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

ImageIcon preg1 = new ImageIcon("C:\\Java\\TestPsicologico\\bin\\Preg1.jpg"); 
╰ゝ天使的微笑 2024-12-05 12:07:06

大家都说得对,只是复制的路径不正确。

您只需将首选图像放入您的项目文件夹
您的图像将显示在项目导航选项卡中,之后只需复制图像路径并将其粘贴到:

final ImageIcon icon = new ImageIcon("*Paste copied path*");


 JOptionPane.showMessageDialog(null, infoMessage, " " + titleBar, JOptionPane.INFORMATION_MESSAGE,icon);

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:

final ImageIcon icon = new ImageIcon("*Paste copied path*");


 JOptionPane.showMessageDialog(null, infoMessage, " " + titleBar, JOptionPane.INFORMATION_MESSAGE,icon);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文