User interface 在区域设置更改时连续设置标签文本时出现问题
在我的JavaFX应用程序中,用户可以选择英语或印地语进行显示。如果我不断地在英语和印地语之间切换,在某个阶段,印地语文本中的空格或特殊字符会被一些水平线所取代,如屏幕截图所示。 我曾尝试在单独的线程中设置标签文本,但没有用。 是因为缓存问题吗?我试图在设置标签文本之前清除它,但仍然面临同样的问题。此外,已禁用标签的缓存属性。有什么建议吗 我指的是博客[Link][1]中的“I18N实用程序类”。每当用户选择语言并触发标签的User interface 在区域设置更改时连续设置标签文本时出现问题,user-interface,javafx,label,javafx-8,scenebuilder,User Interface,Javafx,Label,Javafx 8,Scenebuilder,在我的JavaFX应用程序中,用户可以选择英语或印地语进行显示。如果我不断地在英语和印地语之间切换,在某个阶段,印地语文本中的空格或特殊字符会被一些水平线所取代,如屏幕截图所示。 我曾尝试在单独的线程中设置标签文本,但没有用。 是因为缓存问题吗?我试图在设置标签文本之前清除它,但仍然面临同样的问题。此外,已禁用标签的缓存属性。有什么建议吗 我指的是博客[Link][1]中的“I18N实用程序类”。每当用户选择语言并触发标签的textproperty()时,此类设置语言环境 下面是代码片段: pu
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;
}
}
import java.io.UnsupportedEncodingException;
导入javafx.beans.binding.Bindings;
导入javafx.beans.binding.StringBinding;
导入javafx.beans.property.ObjectProperty;
导入javafx.beans.property.SimpleObject属性;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.Tooltip;
导入java.text.MessageFormat;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Date;
导入java.util.List;
导入java.util.Locale;
导入java.util.ResourceBundle;
导入java.util.concurrent.Callable;
公开期末班i18N{
/**当前选定的区域设置*/
私有静态最终ObjectProperty区域设置;
静止的{
locale=新的SimpleObject属性(getDefaultLocale());
locale.addListener((可观察,oldValue,newValue)->locale.setDefault(newValue));
}
/**
*获取支持的区域设置。
*
*@返回区域设置对象的列表。
*/
公共静态列表getSupportedLocales(){
返回新的ArrayList(Arrays.asList(Locale.ENGLISH,newlocale(“hi”,“IN”)));
//返回新的ArrayList(Arrays.asList(Locale.ENGLISH,newlocale(“hi”,“IN”)));
}
/**
*获取默认区域设置。如果包含在受支持的区域设置中,则这是系统默认设置,否则为英语。
*
*@返回
*/
公共静态区域设置getDefaultLocale(){
Locale sysDefault=Locale.getDefault();
return getSupportedLocales().contains(sysDefault)?sysDefault:Locale.ENGLISH;
}
公共静态区域设置getLocale(){
返回locale.get();
}
公共静态void setLocale(区域设置){
localeProperty().set(区域设置);
setDefault(Locale);
}
公共静态ObjectProperty localeProperty(){
返回区域设置;
}
/**
*从当前区域设置的资源包中获取具有给定键的字符串,并将其用作第一个参数
*到MessageFormat.format,传入可选参数并返回结果。
*
*@param-key
*消息键
*@param args
*消息的可选参数
*@return本地化格式字符串
*/
公共静态字符串get(最终字符串键、最终对象…参数){
/*temp++;
如果(临时%2==0){
}
否则{
}*/
ResourceBundle=ResourceBundle.getBundle(“bundles.lang”,getLocale());
returnmessageformat.format(bundle.getString(key),args);
}
公共静态字符串changeDate()引发不受支持的DencodingException{
SimpleDataFormat日期格式;
dateFormat=新的SimpleDataFormat(“dd-MMM-yyy-E-HH:mm-a”,getLocale());
日期=新日期();
System.out.println(dateFormat.format(date));
返回日期格式。格式(日期);
}
/**
*为给定的消息包密钥创建绑定到本地化字符串的字符串
*
*@param-key
*钥匙
*@返回字符串绑定
*/
公共静态StringBinding createStringBinding(最终字符串键、对象…参数){
返回Bindings.createStringBinding(()->get(key,args),locale);
}
/**
*创建与通过调用给定func计算的本地化字符串的字符串绑定
*
*@param func
*函数在每次更改时调用
*@returnstringbinding
*/
公共静态StringBinding createStringBinding(可调用func){
返回Bindings.createStringBinding(func,locale);
}
/**
*创建绑定标签,其值根据语言更改计算。
*
*@param func
*用于计算值的函数
*@返回标签
*/
公共静态标签labelForValue(可调用func){
标签=新标签();
label.textProperty().bind(createStringBinding(func));
退货标签;
}
/**
*为给定的resourcebundle键创建绑定按钮
*
*@param-key
*ResourceBundle密钥
*@param args
*消息的可选参数
*@返回按钮
*/
公共静态按钮按钮标记(最终字符串键、最终对象…参数){
按钮按钮=新按钮();
button.textProperty().bind(createStringBinding(key,args));
返回按钮;
}
/**
*为给定的resourcebundle键创建绑定工具提示
*
*@param-key
*ResourceBundle密钥
*@param args
*消息的可选参数
*@返回标签
*/
公共静态工具提示tooltipForKey(最终字符串键、最终对象…参数){
工具提示工具提示=新工具提示();
tooltip.textProperty().bind(createStringBinding(key,args));
返回工具提示;
}
}
!![屏幕截图][2]问题已修复:
当前字体样式导致错误。。我将其更改为“Arial”fontstyle,现在它工作正常:)请编辑您的问题,以包含一个显示您所说明问题的选项。@trashgod..Thnx供您参考…我已添加了reference@trashgod... 我