【发布时间】:2017-11-20 00:43:42
【问题描述】:
在我的 JavaFX 应用程序中,用户可以选择英语或印地语进行显示。如果我不断在英语和印地语之间切换,在某一阶段,印地语文本中的空格或特殊字符会被一些水平线替换,如屏幕截图所示。 我尝试在单独的线程中设置标签文本,但没有用。 是因为缓存问题吗?我试图在设置之前清除标签文本,但仍然面临同样的问题。此外,标签的缓存属性被禁用。有什么建议吗?
我指的是博客 [Link][1] 中的“I18N 实用程序类”。每当用户选择语言并触发标签的textproperty() 时,此类都会设置语言环境。
这是代码的sn-p:
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);
}
///////////////// i18N 类: ///////////////
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]
【问题讨论】:
-
请编辑您的问题以包含一个 minimal reproducible example 来展示您说明的问题。
-
@trashgod..Thnx 为您的建议...我已添加代码供参考
-
@trashgod...我还添加了用于动态更改的java文件...现在有什么建议吗?如果我用不同的语言(德语、汉语、日语)替换印地语,我不会遇到任何此类问题..
-
另外,如果我使用文本字段而不是标签,则不会出现问题...但我不想使用文本字段代替标签...
标签: user-interface javafx label javafx-8 scenebuilder