JavaFX 应用程序图标无法显示 9/10 次

JavaFX app icon fails to show 9/10 times

我有一个应用程序可以打开,但无法为其设置图标。我给出路径的图标就在那里,更改为该目录中的另一个想象显示图标 9/10 次,但此图像从未显示。它的位置总是有一个问号。所以即使在另一个我知道可以工作的文件上(即没有损坏),为什么它很少显示?

下面是MyApplication.java

的代码
package MyApp;

import MyApp.Variables.Constants;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Forms/FormMain.fxml"));

        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/img/appicon.png")));
        primaryStage.setTitle("MyApp " + Constants.VERSION_NAME + " (" + Constants.RELEASE_ID + ")"); 
        primaryStage.setScene(new Scene(root, 1000, 800));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

下面是与/img/相关的项目目录结构 Main.java:

我已经尝试了所有解决方案here,但没有解决我的问题。

运行 Ubuntu 16.04,intelliJ IDEA 用于 IDE,但导出的 JAR 文件仍然存在问题。

从磁盘加载数据非常耗时,因此您可以在构建对象时开始加载图标。将它放在构造函数中并将其保存在实例成员中。通常您需要添加多个图标,因为每个平台都需要自己的尺寸(用于链接等)。

package MyApp;

import MyApp.Variables.Constants;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {

    private Image icon;

    public Main() {
      icon = new Image(Main.class.getResource("/img/appicon.png").toExternalForm());
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Forms/FormMain.fxml"));

        primaryStage.getIcons().add(icon);
        primaryStage.setTitle("MyApp " + Constants.VERSION_NAME + " (" + Constants.RELEASE_ID + ")"); 
        primaryStage.setScene(new Scene(root, 1000, 800));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我的图标是这样的: Netbeans 中的应用程序结构如下所示:

运行 应用看起来像这样: