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

Swing框架中图像无法渲染的问题排查求助

图片无法在JFrame的JLabel中显示

我已确认向JFrame添加JLabel的语法正确,且图片路径无误,代码如下:

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JLabel label = new JLabel();
        label.setText("Hello");
        
        ImageIcon nintendoImage = new ImageIcon("Images/nintendo.jpg");
        label.setIcon(nintendoImage);
        frame.add(label);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

我还尝试过以下操作,但问题仍未解决:

  • 使用从Mac访达复制的绝对路径
  • 将图片放入与代码同一包目录中
  • 已导入所有必要的类

我分别在VS Code和IntelliJ中运行代码,均无法正常显示图片。

项目目录截图

内容的提问来源于stack exchange,提问作者Corvus-OS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 22:32:31