在JavaFX 8 DatePicker中更改语言 [英] Changing the language in JavaFX 8 DatePicker

查看:194
本文介绍了在JavaFX 8 DatePicker中更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将DatePicker添加到我的应用程序时,我得到以下内容:

When adding a DatePicker to my app I get the following:

我认为这是因为我在计算机上使用了希伯来语。
如何将DatePicker的语言更改为英语?

I assume this is because I use Hebrew on my computer. How can I change the language of the DatePicker to be English?

推荐答案

您可以为您定义默认语言环境调用Java虚拟机的实例:

You can define the default locale for your instance of the Java Virtual Machine calling:

Locale.setDefault(Locale.ENGLISH);

或者如果你找不到语言环境,你需要在预制的常量中,你可以在官方支持的区域设置列表中查找国家/地区代码并创建这样的自定义语言环境:

Or if you can't find the locale, you need, in the pre made constants, you can look up the country code in the list of officially supported locales and create your "custom" locale like this:

Locale.setDefault(Locale("cs")) //locale for Czech language

。如果您还想为文本编辑器实现自定义格式化程序,则还应将语言环境添加到格式化程序中。

on the start method. If you also want to implement a custom formatter for the text editor, you should add locale to the formatter too.

这只是一个示例:

private final DateTimeFormatter formatter = 
        DateTimeFormatter.ofPattern("EEEE, d.MM.uuuu", Locale.ENGLISH);

@Override
public void start(Stage primaryStage) {
    Locale.setDefault(Locale.ENGLISH);

    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setConverter(new StringConverter<LocalDate>() {

        @Override
        public String toString(LocalDate object) {
            return object.format(formatter);
        }

        @Override
        public LocalDate fromString(String string) {
            return LocalDate.parse(string, formatter);
        }
    });
    StackPane root = new StackPane(datePicker);
    Scene scene = new Scene(root, 400, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}

编辑

按照设计, DatePicker 使用 Locale.getDefault()应用于所有格式弹出窗口上显示的控件。这可以在 com.sun.javafx.scene.control.skin.DatePickerContent class中查看。

By design, DatePicker uses Locale.getDefault() in all the formats applied to the controls displayed on the popup. This can be checked in com.sun.javafx.scene.control.skin.DatePickerContent class.

除非你为控件更改这些格式化程序提供自定义外观,以便将 DatePicker 内容更改为英语,避免进一步更改其他本地化控件,解决方法可能是:

Unless you provide a custom skin for the control changing these formatters, in order to change the DatePicker content to English, avoiding further changes in other localized controls, a workaround could be this:

private final Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);

@Override
public void start(Stage primaryStage) {
    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setOnShowing(e-> Locale.setDefault(Locale.Category.FORMAT,Locale.ENGLISH));
    datePicker.setOnShown(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    ...
}

编辑2

返回 setOnShown 上的原始区域设置太快了,因为如果用户更改月份,则原始区域设置使用,它将无法正确显示。为了工作,应该在 setOnHiding setOnAction 上关闭它。

Returning to the original locale on setOnShown is too soon, since if the user changes the month, the original locale is used and it will not be shown properly. To work it should be turned off both on setOnHiding and on setOnAction.

private final Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);

@Override
public void start(Stage primaryStage) {
    DatePicker datePicker=new DatePicker();
    datePicker.setValue(LocalDate.now());
    datePicker.setOnShowing(e-> Locale.setDefault(Locale.Category.FORMAT,Locale.ENGLISH));
    datePicker.setOnHiding(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    datePicker.setOnAction(e-> Locale.setDefault(Locale.Category.FORMAT,myLocale));
    ...
}

这篇关于在JavaFX 8 DatePicker中更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆