首页 文章

任务栏中的JavaFX图标质量

提问于
浏览
5

在JavaFX中将png图像设置为舞台图标时(使用Kotlin,但这在Java中也是一个问题),图标似乎变形了 .

我搜索了这个问题,发现了以下几点:

  • 这是(可能仍然是)一个错误 . 在这个错误的issue page上,他们解释了一种解决方法是避免使用半透明像素 . 我试过这个,但我的图像仍然会变形,如下所示 .

32x32

Deformed 32x32

Left. 提供给JavaFx的原始32x32映像 . Right. 图像JavaFx放在任务栏中 .

Original 48x48

Deformed 48x48

Left. 提供给JavaFx的原始48x48图像 . Right. 图像JavaFx放在任务栏中 .

看起来32x32必须按比例放大,48x48必须按比例缩小到42x42左右(我也做了42x42,但这也没有帮助) . 由于Windows“想要”的大小要么是2或48x48的大小,你会说那些大小会起作用 .

  • 我遇到了关于这个话题的问题 . 这里讨论的是JavaFX不会自动选择图标的最佳大小,但通常是您添加到图标集的最后一个图标,因此建议将您认为最适合的图标作为最后一个 .

我知道这可能是(可能)JavaFX中一个未解决的错误,而另一个问题是大约三年前的最后一个问题,我想知道是否有人在此期间找到了更好的解决方法 .

我在Kotlin中创建了一个类似于问题页面中提供的MWE,因为您可以轻松地将原始图像与最终在任务栏中的图像进行比较 . 使用的图像如下:

  • icon48.png:
    Original 48x48 icon

  • icon32.png:
    Original 32x32 icon

MWE

class Main : Application() {
    override fun start(primaryStage: Stage) {
        val icon48 = Image("sample/icon48.png")
        val icon32 = Image("sample/icon32.png")
        primaryStage.scene = Scene(Group(
                ImageView(icon48)
                ImageView(icon32)
        ))

        primaryStage.icons.addAll(
                icon48,
                icon32
        )

        primaryStage.show()
    }
}

fun main(args: Array<String>) {
    Application.launch(Main::class.java, *args)
}

1 回答

  • 3

    Windows程序版本的解决方案(最重要的是,这对调试版本没有帮助)是使用launch4j和InnoSetup . launch4j生成一个可执行文件,InnoSetup包装了一个安装程序 .

    How to add a good-looking icon to your JavaFX application for Windows (并使其适用于任务栏)

    1.使用Gradle,添加launch4j插件 . 例如,使用Kotlin Gradle DSL时,请添加

    plugins {
        // Plugin to build .exe files.
        id("edu.sc.seis.launch4j") version "2.4.4"
    }
    
    dependencies {
        // JNA, used to e.g. make a program pinnable to taskbar.
        compile("net.java.dev.jna:jna:4.5.1")
        compile("net.java.dev.jna:jna-platform:4.5.1")
    }
    
    launch4j {
        mainClassName = "nl.mynamespace.myapp.MainKt"
        icon = "$projectDir/src/main/resources/myapp32.ico"
        manifest = "$projectDir/releasing/launch4j/myapp.manifest"
    }
    

    并将图标和清单文件放在您指定的路径中,清单包含

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
        <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
            <security>
                <requestedPrivileges>
                <requestedExecutionLevel level="highestAvailable"
                    uiAccess="False" />
            </requestedPrivileges>
        </security>
        </trustInfo>
    </assembly>
    

    2.运行Gradle任务 launch4j/createExe .

    3.下载InnoSetup .

    4.将任何配置( .iss )文件调整到您的项目,例如如下所示

    #define MyAppName "Myapp"
    #define MyAppVersion "2.0.0-beta.9"
    #define MyAppPublisher "mynamespace"
    #define MyAppURL "https://github.com/mynamespace/myapp"
    #define MyAppExeName "myapp.exe"
    
    [Setup]
    ; NOTE: The value of AppId uniquely identifies this application.
    ; Do not use the same AppId value in installers for other applications.
    ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
    AppId={{something generated here}}
    AppName={#MyAppName}
    AppVersion={#MyAppVersion}
    ;AppVerName={#MyAppName} {#MyAppVersion}
    AppPublisher={#MyAppPublisher}
    AppPublisherURL={#MyAppURL}
    AppSupportURL={#MyAppURL}
    AppUpdatesURL={#MyAppURL}
    DefaultDirName={pf}\{#MyAppName}
    DisableProgramGroupPage=yes
    LicenseFile=..\..\LICENSE
    OutputDir=..\innoSetup
    OutputBaseFilename=setup_myapp_{#MyAppVersion}
    SetupIconFile=..\..\src\main\resources\myapp32.ico
    Compression=lzma
    SolidCompression=yes
    
    [Languages]
    Name: "english"; MessagesFile: "compiler:Default.isl"
    
    [Tasks]
    Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
    
    [Files]
    Source: "..\..\build\launch4j\myapp.exe"; DestDir: "{app}"; Flags: ignoreversion
    ; NOTE: Don't use "Flags: ignoreversion" on any shared system files
    
    [Icons]
    Name: "{commonprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
    Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon; Comment: "Myapp {#MyAppVersion}"; AppUserModelID: "nl.mynamespace.myapp.Main"
    ; create icon shortcut that embeds AppUserModelID information, which is the same as
    ; set in the program, to enable pinning to taskbar.
    
    
    [Run]
    Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: shellexec postinstall skipifsilent
    

    5.在InnoSetup中,单击Build |编译 . 如果您拥有所有路径,那么您现在将拥有一个安装文件,如果您使用它,您将安装JavaFX应用程序,您可以将其固定到任务栏 .

    结果图标:

    nice!

相关问题