2012-09-20 93 views
1

Java中有幾個組件具有預定義的外觀和自動打印在其上的文本字符串。例子是JFileChooser。L&F中字符串鍵的位置

此外,還有一個JDialog(或JOptionPane的)當您嘗試做的JFileChooser illegale重命名彈出...

在什麼*的.java文件(S)可以字符串鍵代表的是鍵和他們在哪裏得到他們的價值?

我在說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)方法拉出琴絃......我試圖尋找它,但我沒有運氣...... 另外,我知道JFil有一些字符串未在BasicFileChooserUI.java中定義的eChooser ...

+1

另請參見[* JDK 6和JRE 6支持的語言環境*](http://www.oracle.com/technetwork/java/javase/locales -137662.html)。 – trashgod

+0

我得到了它的兄弟:-)看到我的編輯,當然有(必須)另一種方式:-) – mKorbel

回答

2

許多此類用戶界面元素已針對支持的語言進行了本地化,如JDK 6 and JRE 6 Supported Locales: User Interface Translation中所示。參考Internationalization: Understanding Locale in the Java Platform。未指定UIManager.getLookAndFeelDefaults()獲取L & F默認值的方式;不支持更改源數據。在返回的Map中找到的屬性的(非本地化)名稱可用於覆蓋默認值。如How to Write a Custom Look and Feel中所述,源文本存儲在每個L和每個支持的語言環境的屬性文件中。 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. 

參見How can I add localization to JFileChooser for a locale that is not supported by default

+0

是的,克羅地亞人是由4.5百萬人口說,我想我必須手動做... 我需要位置所有的字符串初始化... BasicFileChooser包含一些,但不是全部...它只是需要找到文件包含「FileChooser.diferentstringtext」字符串有點類型,這是我發送給UIManager ... –

+0

不知道他是否需要關於Swing及其實現或AWT中的實現的exaplanations,這些Object(在大多數情況下是coumpound)基於來自Native OS的屬性並繼承AWT類的方法 – mKorbel

+0

@Ivan Karlovic爲什麼你需要翻譯標籤和敘述,Locale不起作用,或者你使用一些** nix或solaris OS – mKorbel

2

要改變一個,但我不知道答案,現在

enter image description here

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

你想這個

enter image description here

從代碼

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 Utils,由達里爾·伯克,頂級搖擺大師之一(曾告訴我們,要我爲編程,是如何支付少量孩子舔冰淇淋)

+0

@Ivan Karlovic休息是由你決定的,不要忘記發佈克羅地亞語語言環境到下一個Java更新( - :Java7 :-) – mKorbel

1

這些鍵由Swing PLAF資源包提供,您可以在JDK源中找到它們。參見例如:

對於非英語語言的字符串值由相鄰的包文件提供。

只需爲所需的人類語言創建一個文件並將其放置在類路徑的任何位置,您就可以爲這些系列中的任何一個添加一個包。 .java和.properties格式的工作包同樣適用,但.java格式可能稍微更適合Unicode ...

儘管直接將內容添加到com.sun包中可能違反Java許可證。所以爲了安全起見,它可能是明智的,移動你的額外資源,以一包自己的,並登記其與UIManager這樣的:

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

至於雨雲,我沒有發現什麼特別的資源因此,使用「基本」可以完成這項工作...