Swing 组件如何考虑我的默认语言环境?

How can my default locale be taken into account by the Swing components?

我的默认语言环境是法语,但我的应用程序中的组件仍然是英语。

System.out.println(UIManager.getLookAndFeelDefaults().getDefaultLocale()); // => fr_FR
System.out.println(System.getProperty("user.language")); // => fr

比如我的JFileChooser:

有什么问题吗?有没有办法强制组件语言值?

我正在使用 Java 11、Eclipse、Windows 7.

JFileChooser 用作内容窗格 sun.swing.FilePane。如果您查看 this classinstallDefaults() 方法,您会发现以下评论:

 // TODO: On windows, get the following localized strings from the OS

这意味着它还没有实现(我认为它永远不会实现)。

所以,您唯一能做的(不幸的是)就是使用 UIManager 键手动更改它们:

FileChooser.viewMenuLabelText
FileChooser.refreshActionLabelText
FileChooser.newFolderActionLabelText
FileChooser.listViewActionLabelText
FileChooser.detailsViewActionLabelText
FileChooser.fileSizeKiloBytes
FileChooser.fileSizeMegaBytes
FileChooser.fileSizeGigaBytes
FileChooser.renameErrorTitleText
FileChooser.renameErrorText
FileChooser.renameErrorFileExistsText
FileChooser.lookInLabelText
FileChooser.fileNameLabelText
FileChooser.filesOfTypeLabelText
FileChooser.upFolderToolTipText
FileChooser.newFolderToolTipText
FileChooser.viewMenuButtonToolTipText
FileChooser.saveButtonText
FileChooser.openButtonText
FileChooser.cancelButtonText
FileChooser.updateButtonText
FileChooser.helpButtonText
FileChooser.directoryOpenButtonText
FileChooser.saveButtonToolTipText
FileChooser.openButtonToolTipText
FileChooser.cancelButtonToolTipText
FileChooser.updateButtonToolTipText
FileChooser.helpButtonToolTipText
FileChooser.directoryOpenButtonToolTipText
FileChooser.saveDialogTitleText
FileChooser.openDialogTitleText

例如:

//google translate :)
UIManager.put("FileChooser.newFolderActionLabelText", "créer un nouveau dossier"); 

还有方法JFileChoose#setApproveButtonText,但我认为这不能满足您的需求。

JFileChooser.setDefaultLocale(Locale.getDefault());