簡體   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