我清理 运行 java 项目时没有显示图片

Pictures are not shown when I clean and run java project

我有一个使用 if 语句显示几张图片的简单表单。我在项目目录中有一个名为 "Weather" 的文件夹,它包含我使用的所有图片。当我 运行 来自 NetBeans 的项目时,一切都很好。但是,当我执行 "Clean and Build Project" 然后 运行 导出的 jar 文件时,图片没有显示。我不明白为什么会这样。我已经添加了我在需要时使用的代码。

public void loadWeather() {
    Weather w = new Weather();
    lblWeatherCity.setText(w.getCity());
    lblWeatherTemp.setText(w.getTemp());
    lblWeatherCondition.setText(w.getStatus());

    String weatherCondition =(String) w.getStatus();
    String cloudy = "Cloudy";
    //System.out.println(weatherCondition);



    if(weatherCondition.contains(cloudy)){
        ImageIcon test = new ImageIcon("Weather/Cloudy.jpg"); 
        lblWeatherContitionIcon.setIcon(test);
        lblWeatherContitionIcon.setText(null);

    }else{
        ImageIcon test = new ImageIcon("Weather/academia_logo.jpg"); 
        lblWeatherContitionIcon.setIcon(test);
        lblWeatherContitionIcon.setText(null);

    }     

}

您是否将此目录作为资源添加到您的项目中?当您从 Netbeans 内部启动您的项目时,它可能会工作,因为该目录已找到(因为它相对于应用程序存在 "start directory")。如果您构建一个发行版,Netbeans 需要了解额外的项目资源(图像、图标、本地化文件等)——这些资源将被添加到 jar 中。

Here您可以在 Netbeans 知识库中找到一篇关于如何使用图像/资源的文章。