简体   繁体   English

字符串键在L&F中的位置

[英]Location of String keys in L&F

There are several components in Java that have predefined look and strings of text that are automatically printed on them. Java中有几个组件具有预定义的外观和自动打印在其上的文本字符串。 Examples is JFileChooser. 示例是JFileChooser。

Also, there is a JDialog (or JOptionPane) that pops up when you try to do illegale rename in JFileChooser... 另外,当您尝试在JFileChooser中进行非法重命名时,会弹出一个JDialog(或JOptionPane)...

In what *.java file(s) can string keys that represent that keys and where do they get their values? 在表示哪个* .java文件的字符串键中,代表这些键的字符串以及它们从何处获取值?

I'm talking about Nimbus L&F... I couldn't locate them in Nimbus nor Synth (which doesn't necessary mean they're not there)... I did found JFileChooser Strings in BasicFileChooser. 我在谈论Nimbus L&F ...我无法在Nimbus或Synth中找到它们(这不一定意味着它们不存在)...我确实在BasicFileChooser中找到了JFileChooser字符串。

Bottom line: I'm translating my program and I don't want any surprises, so I'd like to know which components have predefined strings and where to find them, that JDialog from above especially... 底线:我正在翻译程序,我不希望有任何意外,所以我想知道哪些组件具有预定义的字符串以及在哪里可以找到它们,特别是上面的JDialog ...

EDIT: I have found BasicFileChooserUI, and this is one of the methods: 编辑:我找到了BasicFileChooserUI,这是方法之一:

protected void installStrings(JFileChooser fc) {

    Locale l = fc.getLocale();
    newFolderErrorText = UIManager.getString("FileChooser.newFolderErrorText",l);
    newFolderErrorSeparator = UIManager.getString("FileChooser.newFolderErrorSeparator",l);

    newFolderParentDoesntExistTitleText = UIManager.getString("FileChooser.newFolderParentDoesntExistTitleText", l);
    newFolderParentDoesntExistText = UIManager.getString("FileChooser.newFolderParentDoesntExistText", l);

    fileDescriptionText = UIManager.getString("FileChooser.fileDescriptionText",l);
    directoryDescriptionText = UIManager.getString("FileChooser.directoryDescriptionText",l);

    saveButtonText   = UIManager.getString("FileChooser.saveButtonText",l);
    openButtonText   = UIManager.getString("FileChooser.openButtonText",l);
    saveDialogTitleText = UIManager.getString("FileChooser.saveDialogTitleText",l);
    openDialogTitleText = UIManager.getString("FileChooser.openDialogTitleText",l);
    cancelButtonText = UIManager.getString("FileChooser.cancelButtonText",l);
    updateButtonText = UIManager.getString("FileChooser.updateButtonText",l);
    helpButtonText   = UIManager.getString("FileChooser.helpButtonText",l);
    directoryOpenButtonText = UIManager.getString("FileChooser.directoryOpenButtonText",l);

    saveButtonMnemonic   = getMnemonic("FileChooser.saveButtonMnemonic", l);
    openButtonMnemonic   = getMnemonic("FileChooser.openButtonMnemonic", l);
    cancelButtonMnemonic = getMnemonic("FileChooser.cancelButtonMnemonic", l);
    updateButtonMnemonic = getMnemonic("FileChooser.updateButtonMnemonic", l);
    helpButtonMnemonic   = getMnemonic("FileChooser.helpButtonMnemonic", l);
    directoryOpenButtonMnemonic = getMnemonic("FileChooser.directoryOpenButtonMnemonic", l);

    saveButtonToolTipText   = UIManager.getString("FileChooser.saveButtonToolTipText",l);
    openButtonToolTipText   = UIManager.getString("FileChooser.openButtonToolTipText",l);
    cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText",l);
    updateButtonToolTipText = UIManager.getString("FileChooser.updateButtonToolTipText",l);
    helpButtonToolTipText   = UIManager.getString("FileChooser.helpButtonToolTipText",l);
    directoryOpenButtonToolTipText = UIManager.getString("FileChooser.directoryOpenButtonToolTipText",l);
}

I want to know from where is the getString("FileChooser.updateButtonText",l) method pulling out strings... I tried looking for it, but I had no luck... Also, I know there are some strings in JFileChooser that are not defined in BasicFileChooserUI.java... 我想知道getString("FileChooser.updateButtonText",l)方法从何处提取字符串...我试图寻找它,但是我没有运气...另外,我知道JFileChooser中有一些字符串没有在BasicFileChooserUI.java中定义...

Many such user interface elements are already localized for supported languages, as shown in JDK 6 and JRE 6 Supported Locales: User Interface Translation . 许多此类用户界面元素已经针对支持的语言进行了本地化,如JDK 6和JRE 6支持的语言环境:用户界面转换所示

Addenda: See also Internationalization: Understanding Locale in the Java Platform . 附录:另请参见国际化:了解Java平台中的语言环境 The manner in which UIManager.getLookAndFeelDefaults() obtains the L&F defaults is not specified; 未指定UIManager.getLookAndFeelDefaults()获取L&F默认值的方式; changing the source data is not supported. 不支持更改源数据。 The (non-localized) names of the properties found in the returned Map may be used to override the defaults. 在返回的Map找到的属性的(非本地化)名称可以用来覆盖默认值。 As discussed in How to Write a Custom Look and Feel , the source text is stored in a properties file for each L&F and each supported locale. 如何编写自定义外观中所述,源文本存储在每个L&F和每个支持的语言环境的属性文件中。 QuaQua is an example. QuaQua是一个例子。 On my platform, for example, the English strings for com.apple.laf.AquaLookAndFeel are in 例如,在我的平台上, com.apple.laf.AquaLookAndFeel的英语字符串位于

$JAVA_HOME/Resources/English.lproj/aqua.properties

which warns: 警告:

# When this file is read in, the strings are put into the 
# defaults table.  This is an implementation detail of the current
# workings of Swing.  DO NOT DEPEND ON THIS.  This may change in
# future versions of Swing as we improve localization support.

See also How can I add localization to JFileChooser for a locale that is not supported by default ? 另请参见如何为默认情况下不支持的语言环境将本地化添加到JFileChooser

which one you want to change, but I don't know answer now 您想更改哪一个,但我现在不知道答案

在此处输入图片说明

DYM??? DYM ???

look in: 顺便拜访:

file name: 文档名称:

files of type: 文件类型:

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

class ChooserFilterTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                String[] properties = {"os.name", "java.version", "java.vm.version", "java.vendor"};
                for (String property : properties) {
                    System.out.println(property + ": " + System.getProperty(property));
                }
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(null);
                jfc.addChoosableFileFilter(new FileFilter() {

                    @Override
                    public boolean accept(File f) {
                        return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj");
                    }

                    @Override
                    public String getDescription() {
                        return "Wavefront OBJ (*.obj)";
                    }

                    @Override
                    public String toString() {
                        return getDescription();
                    }
                });
                int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(jfc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                jfc.showOpenDialog(null);
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                try {
                    for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            SwingUtilities.updateComponentTreeUI(jfc);
                            break;
                        }
                    }
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                }
                jfc.showOpenDialog(null);
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
            }
        };
        SwingUtilities.invokeLater(r);
    }

    private ChooserFilterTest() {
    }
}

Do you want this one 你要这个吗

在此处输入图片说明

from code 从代码

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.plaf.metal.MetalButtonUI;

public class CrazyFileChooser {

    public static void main(String[] args) {
        try {
            for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        }


        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new CrazyFileChooser().makeUI();
            }
        });
    }

    public void makeUI() {
        JFileChooser chooser = new JFileChooser();
        for (AbstractButton button : SwingUtils.getDescendantsOfType(AbstractButton.class, chooser)) {
            button.setUI(new XORButtonUI());
            button.setForeground(Color.GREEN);
        }
        for (JList list : SwingUtils.getDescendantsOfType(JList.class, chooser)) {
            list.setBackground(Color.PINK);
        }
        JTextField jTextField = SwingUtils.getDescendantOfType(JTextField.class, chooser, "Text", "");
        jTextField.setEditable(false);
        for (JLabel label : SwingUtils.getDescendantsOfType(JLabel.class, chooser)) {
            label.setFont(new Font("Dialog", Font.ITALIC, 18));
            label.setForeground(Color.RED);
        }
        chooser.showOpenDialog(null);
    }
}

class XORButtonUI extends MetalButtonUI {

    @Override
    public void paint(Graphics g, JComponent c) {
        g.setXORMode(Color.YELLOW);
        super.paint(g, c);
    }
}

based on code Swing Utils , by Darryl Burke, one of top Swing gurus (once told us, to pay me for the programming, is how to pay a small child for licking ice cream) 是基于Swing顶级专家之一的Darryl Burke的代码Swing Utils编写的(曾经告诉我们,要付钱给我买程序,是如何付钱给一个小孩舔舔冰淇淋的)

These keys are provided by Swing PLAF resource bundles, and you can find them in the JDK sources. 这些密钥由Swing PLAF资源包提供,您可以在JDK源代码中找到它们。 See eg: 参见例如:

String values for languages other than English are provided by adjacent bundle files. 相邻捆绑文件文件提供了英语以外的其他语言的字符串值。

And you can add one more bundle to any of these families just by creating one more file for desired human language and placing it anywhere on your classpath. 您只需为所需的人类语言再创建一个文件并将其放置在类路径中的任何位置,就可以为这些家族中的任何一个添加一个捆绑包。 Bundles in .java and .properties format work equally well, though .java format may be slightly more Unicode-friendly... .java和.properties格式的捆绑包同样工作良好,尽管.java格式可能对Unicode更友好...

It may be good to keep in mind though that direct adding of content to com.sun package may violate the Java license. 尽管将内容直接添加到com.sun程序包可能会违反Java许可证,但请记住一个好方法。 So to be on the safe side, it may be wise to move your extra resources to a package of your own and register it with UIManager like this: 为了安全起见,明智的做法是将您的额外资源移到您自己的程序包中,然后像这样在UIManager注册它:

UIManager.getDefaults().addResourceBundle("mypackage.swing.plaf.basic.resources.basic");

And as for Nimbus, I did not find any special resources for it, so going with "basic" may do the job... 至于Nimbus,我没有找到任何特殊的资源,因此选择“基本”可能会做...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM