【问题标题】:How to fix NumberFormat format even after packaging the application即使在打包应用程序后如何修复 NumberFormat 格式
【发布时间】:2025-11-21 21:40:01
【问题描述】:

我正在使用 NumberFormat 将我的十进制数字格式化为意大利格式 (10000 -> 10.000),按预期工作,但是当我使用 Jlink badass 插件打包我的应用程序时,它以美国格式显示所有数字 (10,000) (如果我在代码中选择意大利格式)

为了简化问题,我制作了一个简单的 hello world 应用程序来说明问题:

主类

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

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

控制器

public class HelloController {
    @FXML
    private Label welcomeText;

    @FXML
    protected void onHelloButtonClick() {
        NumberFormat nf = NumberFormat.getNumberInstance(Locale.ITALIAN);
        DecimalFormat formatter = (DecimalFormat) nf;
        formatter.applyPattern("#,###");
        welcomeText.setText(formatter.format(56465465));
    }
}

Fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
      fx:controller="com.example.demo4.HelloController">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
    </padding>

    <Label fx:id="welcomeText"/>
    <Button text="Hello!" onAction="#onHelloButtonClick"/>
</VBox>

Gradle.Build

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.10'
    id 'org.beryx.jlink' version '2.24.4'
}

group 'com.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

ext {
    junitVersion = '5.7.1'
}

sourceCompatibility = '17'
targetCompatibility = '17'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'com.example.demo4'
    mainClass = 'com.example.demo4.Runner'
}

javafx {
    version = '17-ea+11'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {

    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}

test {
    useJUnitPlatform()
}


jlink {
    jpackage{
        imageOptions = ["--icon", "C:/demo4/src/main/resources/com/example/demo4/Icon.ico"]
    }
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'Karrty'
    }
}

结果

编译执行代码时,标签显示:56.465.465

运行 .exe 文件(由 Jlink badass 插件创建)时,标签显示:56,465,465

【问题讨论】:

  • @jewelsea 你是对的,另一种方法(如果你的项目是模块化的)是在项目模块信息中添加“需要 jdk.localedata”。谢谢你的帮助
  • 用答案替换了 cmets。

标签: java javafx java-11 jlink jpackage


【解决方案1】:

jlink 有一个--include-locales 选项,您应该包含该选项以适当地本地化您的 jlink 映像安装:

查看 jlink 的手册页:

选项

--include-locales=langtag[,langtag]* 

说明

包括语言环境列表,其中 langtag 是 BCP 47 语言标记。 该选项支持 RFC 4647 中定义的语言环境匹配。确保添加模块 jdk.localedata 使用此选项时。

例子

--add-modules jdk.localedata --include-locales=en,ja,*-IN

正如 Youssef Idraiss 在 cmets 中所指出的,如果您的应用程序有一个 module-info.java 文件,而不是将 jdk.localedata 模块添加为命令行选项,您可以在您的 module-info.java 文件中添加 require the module .

要在 badass gradle 插件中使用,您可以将适当的选项传递给插件,例如

jlink {
    jpackage{
        imageOptions = ["--icon", "C:/demo4/src/main/resources/com/example/demo4/Icon.ico"]
    }
    options = ['--include-locales=en,ja,*-IN', '--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'Karrty'
    }
}

【讨论】: