You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中showMessageDialog()无法设置自定义图标问题及解决

问题:JOptionPane对话框自定义图标无法显示

我尝试在showMessageDialog()方法中设置自定义图标,代码如下:

public class MessageDialog1 {
    public static void main(String[] args) {
            ImageIcon img = new ImageIcon("luffy.png");
            JOptionPane.showMessageDialog(null, "Wrong Password Fardeen!", "Custom Icon", JOptionPane.PLAIN_MESSAGE, img);
    }
}

但对话框仅显示默认的普通图标:
原对话框效果


解决方法

通过添加getResource()方法解决了该问题,修复后的对话框效果:
修复后的对话框效果

更新后的代码:

ImageIcon img =  new ImageIcon(MessageDialog1.class.getResource("luffy.png"));
JOptionPane.showMessageDialog(null, "Wrong Password Fardeen!", "Custom Icon", JOptionPane.PLAIN_MESSAGE, img);

内容的提问来源于stack exchange,提问作者meteorboyf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:54:55