【发布时间】:2016-11-05 11:25:22
【问题描述】:
我有一个应用程序可以正常打开,但无法为其设置图标。我提供路径的图标在那里,并且在该目录中更改为另一个图像会显示该图标 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 上运行,用于 IDE 的 IntelliJ IDEA,但导出的 JAR 文件仍然存在问题。
【问题讨论】:
-
如果有效,请使用 [new Image("File:/img/appicon.png")]。还要检查这个download.java.net/jdk8/jfxdocs/javafx/scene/image/Image.html,因为你必须使用[new Image(getClass().getResourceAsStream("/img/appicon.png");]
-
恐怕这行不通。我已经查看了文档,但我不确定能从中获得什么,我尝试使用各种构造函数,传入流而不是字符串,后台加载,没有任何效果。
-
当您将其导出为 jar 时,您必须使用 new Image(getClass().getResourceAsStream("/img/appicon.png"); 将其作为流获取你的项目建成了吗?
-
图片已添加到主帖。我已将代码更改为使用 getClass().getResourceAsStream(...) 但仍然无济于事。
-
使用构造函数
Main()并在变量中加载带有new Image(MainApp.class.getResource(filename with extension).toExternalForm())的图标。在您的start()方法中,将变量添加到您的图标中。
标签: java image javafx icons stage