java 如何更改 JFileChooser 中的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4850315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:18:28  来源:igfitidea点击:

How to change text in JFileChooser?

javaswingjfilechooser

提问by VextoR

In JFileChooserJava swing component I need to change all text elements (for translation):

JFileChooserJava swing 组件中,我需要更改所有文本元素(用于翻译):

File name:(JLabel)

文件名:(JLabel)

Files of type:(JLabel)

文件类型:(JLabel)

Cancel(JButton)

取消(J按钮)

Unfortunately, it is not any methods for that..

不幸的是,这不是任何方法。

Is there any way I can do it?

有什么办法可以做到吗?

Thanks!

谢谢!

回答by Vincent Ramdhanie

The Swing components in Java are fully able to understand internationalization. This articleexplains the details and shows an example of how it can be accomplished.

Java 中的 Swing 组件完全能够理解国际化。本文解释了详细信息并展示了如何实现的示例。

回答by Lenchik

UIManager.put("FileChooser.fileNameLabelText", "FileName");
UIManager.put("FileChooser.filesOfTypeLabelText", "TypeFiles");

回答by Riduidel

If you only need to translate JFileChoosertext, I would suggest you change JFileChooserlocale (by calling JFileChooser#setLocale(Locale)) instead of hacking you way into JFileChooserits internals. Indeed, all JFileChoosertexts are locale dependant. As a consequence, changing locale to be the one you want will alter these texts with less efforts.

如果您只需要翻译JFileChooser文本,我建议您更改JFileChooser语言环境(通过调用JFileChooser#setLocale(Locale))而不是侵入JFileChooser其内部。实际上,所有JFileChooser文本都取决于语言环境。因此,将语言环境更改为您想要的语言环境将更轻松地更改这些文本。

回答by Tamil

The showDialog()is used to display a custom dialog (e.g. not an Open nor Save dialog). It has a parameter to specify the text of the Approve button. If the title has not been set with the setDialogTitle()method, the implementation arbitrarily chooses to use the approve button's text as the title on Windows OS, however this is not documented anywhere and you should not count on this to work.

showDialog()用于显示定制对话框(例如不是开放也不保存对话框)。它有一个参数来指定批准按钮的文本。如果没有使用该setDialogTitle()方法设置标题,则实现会任意选择使用批准按钮的文本作为 Windows 操作系统上的标题,但是这在任何地方都没有记录,您不应该指望这会起作用。

If you want a custom title, use setDialogTitle(). If you want a custom approve button text, use setApproveButtonText(). Obviously showDialog()also takes the approve button's text in which case you do not need to call setApproveButtonText()prior.

如果您想要自定义标题,请使用setDialogTitle(). 如果您想要自定义批准按钮文本,请使用setApproveButtonText(). 显然showDialog()也需要批准按钮的文本,在这种情况下,您不需要setApproveButtonText()事先调用。

If you want an Open dialog, use the showOpenDialog()method. If you want a Save dialog, use the showSaveDialog(). Only use showDialog()if you want a custom dialog.

如果您想要打开对话框,请使用该showOpenDialog()方法。如果需要保存对话框,请使用showSaveDialog(). 仅showDialog()当您想要自定义对话框时才使用。

回答by Agi Hammerthief

This answer is a modified version of one posted by Fasimba/Icewalker on the DevX Java forum. 1I have quoted his answer, only modifying the search and replace parameters. I do nottake credit for the logic expressed in it.

该答案是 Fasimba/Icewalker 在DevX Java 论坛上发布的答案的修改版本。1我引用了他的回答,只修改了搜索和替换参数。我相信其中表达的逻辑。

public void changeButtonText (Component c, String original, String change) {

   if (c instanceof JButton) {
       JButton b = (JButton) c;
       if (b.getText() != null && b.getText().equalsIgnoreCase(original))
           b.setText(change);
   } else if (c instanceof Container) {
        Container cont = (Container) c;
        for (int i = 0; i < cont.getComponents().length; i++) {
           changeButtonText (cont.getComponent(i), original, change);
        }
   }
}

Call it as follows:

调用如下:

// dirChooser is the JFileChooser instance
for (Component c : dirChooser.getComponents()) changeButtonText(c, "Cancel", "Don't do it!");


  1. Posted to the DevX Java forumon 23/06/2004, 08:06 AM
  1. 发表于DevX Java 论坛于 23/06/2004, 08:06 AM

回答by Ronald

Use UIManager

使用 UIManager

    UIManager.put("FileChooser.saveButtonText","Custom text acept");
    UIManager.put("FileChooser.cancelButtonText","custom text to cancel");
    JFileChooser fileChooser = new JFileChooser();

回答by Aaron

Normally JFileChooser@setLocale(Locale)works as Riduidel says. On Mac OSX though this is ignored.

通常JFileChooser@setLocale(Locale)像Riduidel所说的那样工作。在 Mac OSX 上,虽然这被忽略了。

On Mac OSX to correctly set the default locale you call UIManager.getLookAndFeelDefaults().setDefaultLocale(Locale);Note that this only works for Java 8 and not for Java 7!

在 Mac OSX 上正确设置您调用的默认语言环境 请UIManager.getLookAndFeelDefaults().setDefaultLocale(Locale);注意,这仅适用于 Java 8 而不适用于 Java 7!

It seems that even though for every other Look and Feel you use UIManager.getDefaults().setDefaultLocale(Locale);for the Aqua look and feel this doesn't work. It looks like in AquaFileChooserUI.javain the method protected void installStrings(JFileChooser paramJFileChooser)the calls to UIManager.getString()do not use the locale whereas other installStrings() methods, for example in BasicFileChooserUI, do use the locale in the protected void installStrings(JFileChooser)method.

似乎即使对于您UIManager.getDefaults().setDefaultLocale(Locale);用于 Aqua 外观的所有其他外观和感觉,这也不起作用。看起来在AquaFileChooserUI.java方法protected void installStrings(JFileChooser paramJFileChooser)中调用UIManager.getString()不使用语言环境,而其他 installStrings() 方法(例如在 BasicFileChooserUI 中)在protected void installStrings(JFileChooser)方法中使用语言环境。

Aqua: UIManager.getString("FileChooser.cancelButtonText");

水: UIManager.getString("FileChooser.cancelButtonText");

Basic: UIManager.getString("FileChooser.cancelButtonText",l);

基本的: UIManager.getString("FileChooser.cancelButtonText",l);

fileChooser.setLocale(Locale);is still ignored on OSX though.

fileChooser.setLocale(Locale);虽然在 OSX 上仍然被忽略。