繁体   English   中英

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

[英]Location of String keys in L&F

Java中有几个组件具有预定义的外观和自动打印在其上的文本字符串。 示例是JFileChooser。

另外,当您尝试在JFileChooser中进行非法重命名时,会弹出一个JDialog(或JOptionPane)...

在表示哪个* .java文件的字符串键中,代表这些键的字符串以及它们从何处获取值?

我在谈论Nimbus L&F ...我无法在Nimbus或Synth中找到它们(这不一定意味着它们不存在)...我确实在BasicFileChooser中找到了JFileChooser字符串。

底线:我正在翻译程序,我不希望有任何意外,所以我想知道哪些组件具有预定义的字符串以及在哪里可以找到它们,特别是上面的JDialog ...

编辑:我找到了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);
}

我想知道getString("FileChooser.updateButtonText",l)方法从何处提取字符串...我试图寻找它,但是我没有运气...另外,我知道JFileChooser中有一些字符串没有在BasicFileChooserUI.java中定义...

许多此类用户界面元素已经针对支持的语言进行了本地化,如JDK 6和JRE 6支持的语言环境:用户界面转换所示

附录:另请参见国际化:了解Java平台中的语言环境 未指定UIManager.getLookAndFeelDefaults()获取L&F默认值的方式; 不支持更改源数据。 在返回的Map找到的属性的(非本地化)名称可以用来覆盖默认值。 如何编写自定义外观中所述,源文本存储在每个L&F和每个支持的语言环境的属性文件中。 QuaQua是一个例子。 例如,在我的平台上, com.apple.laf.AquaLookAndFeel的英语字符串位于

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

警告:

# 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.

另请参见如何为默认情况下不支持的语言环境将本地化添加到JFileChooser

您想更改哪一个,但我现在不知道答案

在此处输入图片说明

DYM ???

顺便拜访:

文档名称:

文件类型:

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() {
    }
}

你要这个吗

在此处输入图片说明

从代码

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);
    }
}

是基于Swing顶级专家之一的Darryl Burke的代码Swing Utils编写的(曾经告诉我们,要付钱给我买程序,是如何付钱给一个小孩舔舔冰淇淋的)

这些密钥由Swing PLAF资源包提供,您可以在JDK源代码中找到它们。 参见例如:

相邻捆绑文件文件提供了英语以外的其他语言的字符串值。

您只需为所需的人类语言再创建一个文件并将其放置在类路径中的任何位置,就可以为这些家族中的任何一个添加一个捆绑包。 .java和.properties格式的捆绑包同样工作良好,尽管.java格式可能对Unicode更友好...

尽管将内容直接添加到com.sun程序包可能会违反Java许可证,但请记住一个好方法。 为了安全起见,明智的做法是将您的额外资源移到您自己的程序包中,然后像这样在UIManager注册它:

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

至于Nimbus,我没有找到任何特殊的资源,因此选择“基本”可能会做...

暂无
暂无

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

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