当前位置: 首页 > 知识库问答 >
问题:

JavaFX应用程序图标未能显示9/10次

慕容恩
2023-03-14

我有一个应用程序,它可以完全打开,但我在为它设置图标时遇到了麻烦。我给出路径的图标就在那里,在该目录中切换到另一个imagine会显示图标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的项目目录结构:

我在这里尝试了所有的解决方案,但都没有解决我的问题。

运行在Ubuntu16.04上,intelliJ IDEA用于IDE,但问题仍然存在于导出的JAR文件中。

共有1个答案

东明德
2023-03-14

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

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);
    }
}

跑步应用程序看起来是这样的:

 类似资料:
  • 是否可以使用JavaFX更改应用程序图标,还是必须使用Swing来完成?

  • 我已经测试了http://api.jquerymobile.com/loader/的代码。

  • 我刚刚在play store上发布了我的应用程序,我注意到一些三星设备没有显示我应用程序的图标,而是使用了android Studio的默认图标。你知道是什么引起的吗?我很困惑

  • 我有一个装满图像的文件夹要在我正在创建的游戏中实现,我想在窗口中显示这些。这是我迄今为止的代码。 这将是一个程序在IntelliJ/中从jar运行。“资产”文件夹与此文件位于同一目录中。我没有发现文件路径错误,所以我假设它可以找到图像,但它不会出现在屏幕上,而矩形会出现。 公平警告,我正在从头开始学习JavaFX,我发现没有太多关于事物如何工作的解释,所以这可能是一个愚蠢的问题。

  • 或者 我还尝试在没有文件的情况下实现这一点,结果成功了。 如何添加带有文件的应用程序图标?

  • 我是Spring靴的初学者。我制作了简单的crud应用程序,但在编译项目时没有任何错误,但在浏览器中看不到输出。我不知道为什么。 只有这样显示才能在浏览器中看到输出。我不知道为什么。 员工控制员看起来像这样 Index.html porm.xml spring.jpa.hibernate.ddl-car=无 spring.datasource.url=jdbc:mysql://localhost: