【发布时间】: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