2017-06-17 85 views
0

在我的JavaFX應用程序中,用戶可以選擇英語或印地語進行顯示。如果我不斷地在英語和印地語之間切換,在一個階段,印地語文本中的空格或特殊字符會被一些水平線替換,如截圖所示。 我已經嘗試在單獨的線程中設置標籤文本,但沒有用。 是否由於緩存問題?我試圖在設置之前清除標籤文本,但仍面臨同樣的問題。此外,標籤的緩存屬性被禁用。有什麼建議麼?在區域設置上連續設置標籤文本時發出

我指的是博客[Link] [1]中的'I18N實用程序類'。只要用戶選擇了語言,此類就設置語言環境,並觸發標籤的textproperty()

這裏是代碼片段:

public class example implements Initializable { 
    @FXML 
    private Label dateLbl; 

    public void initialize(URL url, ResourceBundle rb) { 
      engBtn.setOnAction((evt) -> switchLanguage(Locale.ENGLISH)); 
      hnBtn.setOnAction((evt) -> switchLanguage(new Locale("hi","IN"))); 
      dateLbl.textProperty().bind(createStringBinding(() -> 
       I18N.changeDate())); //DATE LABEL WHICH IS SHOWING WEIRD BEHAVIOUR 
    } 

    private void switchLanguage(Locale locale) { 
     I18N.setLocale(locale); 
    } 

/////////////////// 類國際化: /////// !/////////

import java.io.UnsupportedEncodingException; 
import javafx.beans.binding.Bindings; 
import javafx.beans.binding.StringBinding; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.Tooltip; 

import java.text.MessageFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Date; 
import java.util.List; 
import java.util.Locale; 
import java.util.ResourceBundle; 
import java.util.concurrent.Callable; 


public final class i18N { 
    /** the current selected Locale. */ 
    private static final ObjectProperty<Locale> locale; 
    static { 
     locale = new SimpleObjectProperty<>(getDefaultLocale()); 
     locale.addListener((observable, oldValue, newValue) -> Locale.setDefault(newValue)); 
    } 

    /** 
    * get the supported Locales. 
    * 
    * @return List of Locale objects. 
    */ 
    public static List<Locale> getSupportedLocales() { 
return new ArrayList<>(Arrays.asList(Locale.ENGLISH, new Locale("hi","IN")));   
//return new ArrayList<>(Arrays.asList(Locale.ENGLISH, new Locale("hi","IN"))); 
    } 

    /** 
    * get the default locale. This is the systems default if contained in the supported locales, english otherwise. 
    * 
    * @return 
    */ 
    public static Locale getDefaultLocale() { 
     Locale sysDefault = Locale.getDefault(); 
     return getSupportedLocales().contains(sysDefault) ? sysDefault : Locale.ENGLISH; 
    } 

    public static Locale getLocale() { 
     return locale.get(); 
    } 

    public static void setLocale(Locale locale) { 
     localeProperty().set(locale); 
     Locale.setDefault(locale); 
    } 

    public static ObjectProperty<Locale> localeProperty() { 
     return locale; 
    } 

    /** 
    * gets the string with the given key from the resource bundle for the current locale and uses it as first argument 
    * to MessageFormat.format, passing in the optional args and returning the result. 
    * 
    * @param key 
    *   message key 
    * @param args 
    *   optional arguments for the message 
    * @return localized formatted string 
    */ 
    public static String get(final String key, final Object... args) { 
     /*temp++; 
     if(temp%2==0){ 
     } 
     else { 

     }*/ 
     ResourceBundle bundle = ResourceBundle.getBundle("bundles.lang", getLocale()); 
     return MessageFormat.format(bundle.getString(key), args); 
    } 


    public static String changeDate() throws UnsupportedEncodingException { 

     SimpleDateFormat dateFormat; 
     dateFormat = new SimpleDateFormat("dd-MMM-yyyy E HH:mm a",getLocale()); 
     Date date = new Date(); 
     System.out.println(dateFormat.format(date)); 
     return dateFormat.format(date); 

    } 

    /** 
    * creates a String binding to a localized String for the given message bundle key 
    * 
    * @param key 
    *   key 
    * @return String binding 
    */ 
    public static StringBinding createStringBinding(final String key, Object... args) { 
     return Bindings.createStringBinding(() -> get(key, args), locale); 
    } 

    /** 
    * creates a String Binding to a localized String that is computed by calling the given func 
    * 
    * @param func 
    *   function called on every change 
    * @return StringBinding 
    */ 
    public static StringBinding createStringBinding(Callable<String> func) { 
     return Bindings.createStringBinding(func, locale); 
    } 

    /** 
    * creates a bound Label whose value is computed on language change. 
    * 
    * @param func 
    *   the function to compute the value 
    * @return Label 
    */ 
    public static Label labelForValue(Callable<String> func) { 
     Label label = new Label(); 
     label.textProperty().bind(createStringBinding(func)); 
     return label; 
    } 

    /** 
    * creates a bound Button for the given resourcebundle key 
    * 
    * @param key 
    *   ResourceBundle key 
    * @param args 
    *   optional arguments for the message 
    * @return Button 
    */ 
    public static Button buttonForKey(final String key, final Object... args) { 
     Button button = new Button(); 
     button.textProperty().bind(createStringBinding(key, args)); 
     return button; 
    } 

    /** 
    * creates a bound Tooltip for the given resourcebundle key 
    * 
    * @param key 
    *   ResourceBundle key 
    * @param args 
    *   optional arguments for the message 
    * @return Label 
    */ 
    public static Tooltip tooltipForKey(final String key, final Object... args) { 
     Tooltip tooltip = new Tooltip(); 
     tooltip.textProperty().bind(createStringBinding(key, args)); 
     return tooltip; 
    } 

} 

[截圖] [2]

+0

請編輯您的問題,包括一個[mcve],展示您說明的問題。 – trashgod

+0

@ trashgod..Thnx爲您的建議...我添加了參考代碼 – k129

+0

@trashgod ...我還添加了我用於動態更改的Java文件...現在有什麼建議? 如果我用不同的語言(德語,中文,日語)替換印地文,我不會遇到任何這樣的問題。 – k129

回答

0

發出定額:

當前fontstyle是CAU的ing錯誤..我改變它'Arial'fontstyle現在它工作正常:)