在我的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);
}
/////////////////// 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]
答案 0 :(得分:0)
问题已解决:
当前fontstyle导致错误..我将其更改为&#39; Arial&#39; fontstyle现在它正常工作:)