如何在 JavaFX 中设置应用程序安装程序图标?

How to set application installer icon in JavaFX?

我正在使用 JavaFX-Gradle-plugin 构建可分发的二进制文件和 JavaFX 应用程序的安装程序。当我的应用程序运行时,我可以这样设置图标:

stage.getIcons().add(new Image(this.getClass().getResourceAsStream("/isotype.png")));

正确设置 运行 应用程序的图标:

还有任务栏:

但是如何设置开始菜单的图标:

可能还有其他地方:

有一个公开的拉取请求记录了这个 here

它说:

Customize Icons

To customize the icons used in a native bundle, you have to provide the icons for the appropriate bundle. The icons must follow the file name convention in order to get picked up.

Tip: Set the verbose setting to true, to have log which files are picked up from your deploy directory.

特别是对于 Microsoft Windows:

Windows

Icon location: src/main/deploy/windows

For Windows you can provide two different icons.

  • application icon
  • setup icon - the icon of the installer

| Type | Filename | | :---------------- |:------------------------- | | .exe icon | \<appName>.ico | | setup exe icon | \<appName>-setup-icon.bmp |

尽管它在那里说了什么,正确的路径是 src/main/deploy/packages/windows,如 adjusted-launcher-icon example.

中所示

可能你的图片路径("/isotype.png")不正确。从以下选项中选择一种方法给出正确的路径。如果存储图标图像:

  • 在文件夹(例如图像)中然后使用此路径 "/images/isotype.png" 就像:

    stage.getIcons().add(
          new Image(this.getClass().getResourceAsStream("/images/isotype.png")));
    
  • 在包目录中,然后使用此路径 "isotype.png",如下所示:

    stage.getIcons().add(new Image(this.getClass().getResourceAsStream("isotype.png")));
    
  • 在文件夹结构中,然后使用此路径 "../images/isotype.png",如下所示:

    stage.getIcons().add(
          new Image(this.getClass().getResourceAsStream("../images/isotype.png"")));
    

已更新:

你得看看A guide to the Gradle JavaFX Plugin which describes the Javafx packages are complete with cross-platform flair-like start menu integration, dock and tray icons, menu-bar integration, and single click icons. For that you've to Sign your files in the output folder if you plan to distribute the application, stated here in 7.3.5 using signtool.exe

现在您必须在 build.gradle 中设置一些(图标)配置选项,如:

javafx {
    appID 'SampleApp'
    appName 'Sample Application'
    mainClass 'com.example.sample.Main'

    jvmArgs = ['-XX:+AggressiveOpts', '-XX:CompileThreshold=1']
    systemProperties = [ 'prism.disableRegionCaching':'true' ]
    arguments = ['-l', '--fast']

    embedLauncher = false

    // deploy/info attributes
    category = 'Demos'
    copyright = 'Copyright (c) 2013 Acme'
    description = 'This is a sample configuration, it is not real.'
    licenseType = 'Apache 2.0'
    vendor = 'Acme'
    installSystemWide = true
    menu = true
    shortcut = true

    // app icons
    icons {
        shortcut = ['shortcut-16.png', 'shortcut-32.png', 'shortcut-128.png', 'shortcut-256.png', 'shortcut-16@2x.png', 'shortcut-32@2x.png', 'shortcut-128@2x.png']
        volume = 'javafx-icon.png'
        setup = 'javafx-icon.png'
    }

    // applet and webstart stuff
    debugKey {
        alias = 'debugKey'
        //keyPass = 'password' // provide via command line
        keyStore = file('~/keys/debug.jks')
        //storePass = 'password'  // provide via command line
    }
    releaseKey {
        alias = 'production'
        //keyPass = 'password' // provide via command line
        keyStore = file('/Volumes/ProdThumbDrive/production.jks')
        //storePass = 'password'  // provide via command line
    }
    signingMode 'release'

    width = 800
    height = 600
    embedJNLP = false
    codebase = 'http://example.com/bogus/JNLP/Codebase'

    // arbitrary jnlp icons
    icon {
        href = 'src/main/resources/javafx-icon.png'
        kind = 'splash'
        width = 128
        height = 128
    }
    icon {
        href = 'shortcut-32@2x.png'
        kind = 'selected'
        width = 16
        height = 16
        scale = 1
    }
}

此处记录了如何执行此操作的一般过程: https://github.com/BilledTrain380/javafx-gradle-plugin/blob/648acafa7198e9bd7cf1a2ef933456ce5e0b65f9/README.md#customize-icons 但最近我在使用最新版本的打包程序(实际上是 ant 任务)时遇到了问题,无法让它正常工作。那里似乎有什么东西被打破了,因为它适用于旧版 (Java 8) 的打包程序,但不适用于最新版本。但是,我能够通过明确指定

来让它工作
<fx:bundleArgument arg="icon" value="package/macosx/myapp.icns"/>

在 fx:deploy 部分。我不知道如何在 Gradle 中做到这一点,因为我使用了 ant,但你应该能够找到它。在旧版本的打包器中,这是没有必要的。

如果您使用 ant build 或 artifact 来构建 javafx 应用程序,请遵循 post 可能会有所帮助

https://flaironix.com/2019/09/18/adding-custom-icon-for-javafx-application-exe-file-in-intelije/

在工件中使用此选项标签

<option name="icons">
    <JavaFxApplicationIcons>
    <option name="linuxIcon" value="$PROJECT_DIR$/src/Controller/logo.png" />
    <option name="windowsIcon" value="$PROJECT_DIR$/src/Controller/logo.ico"/>
   </JavaFxApplicationIcons>
</option>