JavaFX:在运行时更改应用程序语言

JavaFX:在运行时更改应用程序语言,第1张

JavaFX:在运行时更改应用程序语言

你可以做这样的事情。正如您在回答中一样,您可能希望将其实现为单例,或者使用DI框架在需要的地方注入单个实例:

public class ObservableResourceFactory {    private ObjectProperty<ResourceBundle> resources = new SimpleObjectProperty<>();    public ObjectProperty<ResourceBundle> resourcesProperty() {        return resources ;    }    public final ResourceBundle getResources() {        return resourcesProperty().get();    }    public final void setResources(ResourceBundle resources) {        resourcesProperty().set(resources);    }    public StringBinding getStringBinding(String key) {        return new StringBinding() { { bind(resourcesProperty()); } @Override public String computevalue() {     return getResources().getString(key); }        };    }}

现在,您可以执行以下 *** 作:

ObservableResourceFactory resourceFactory = .... ;resourceBundle.setResources(...);Label greetingLabel = new Label();greetingLabel.textProperty().bind(resourceFactory.getStringBinding("greeting"));

而且任何时候您使用

resourceFactory.setResources(...);

将导致标签更新其文本。

这是一个SSCCE(对将a强制

ResourceBundle
为单个可运行类的极其丑陋的方式表示歉意…)

import java.util.ListResourceBundle;import java.util.Locale;import java.util.ResourceBundle;import javafx.application.Application;import javafx.beans.binding.StringBinding;import javafx.beans.property.ObjectProperty;import javafx.beans.property.SimpleObjectProperty;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.ComboBox;import javafx.scene.control.Label;import javafx.scene.control.ListCell;import javafx.scene.layout.BorderPane;import javafx.stage.Stage;public class ResourceBundleBindingExample extends Application {    private static final String RESOURCE_NAME = Resources.class.getTypeName() ;    private static final ObservableResourceFactory RESOURCE_FACTORY = new ObservableResourceFactory();    static {        RESOURCE_FACTORY.setResources(ResourceBundle.getBundle(RESOURCE_NAME));    }    @Override    public void start(Stage primaryStage) {        ComboBox<Locale> languageSelect = new ComboBox<>();        languageSelect.getItems().addAll(Locale.ENGLISH, Locale.FRENCH);        languageSelect.setValue(Locale.ENGLISH);        languageSelect.setCellFactory(lv -> new LocaleCell());        languageSelect.setButtonCell(new LocaleCell());        languageSelect.valueProperty().addListener((obs, oldValue, newValue) -> { if (newValue != null) {     RESOURCE_FACTORY.setResources(ResourceBundle.getBundle(RESOURCE_NAME, newValue)); }        });        Label label = new Label();        label.textProperty().bind(RESOURCE_FACTORY.getStringBinding("greeting"));        BorderPane root = new BorderPane(null, languageSelect, null, label, null);        root.setPadding(new Insets(10));        Scene scene = new Scene(root, 400, 400);        primaryStage.setScene(scene);        primaryStage.show();    }    public static class LocaleCell extends ListCell<Locale> {        @Override        public void updateItem(Locale locale, boolean empty) { super.updateItem(locale, empty); if (empty) {     setText(null); } else {     setText(locale.getDisplayLanguage(locale)); }        }    }    public static class ObservableResourceFactory {        private ObjectProperty<ResourceBundle> resources = new SimpleObjectProperty<>();        public ObjectProperty<ResourceBundle> resourcesProperty() { return resources ;        }        public final ResourceBundle getResources() { return resourcesProperty().get();        }        public final void setResources(ResourceBundle resources) { resourcesProperty().set(resources);        }        public StringBinding getStringBinding(String key) { return new StringBinding() {     { bind(resourcesProperty()); }     @Override     public String computevalue() {         return getResources().getString(key);     } };        }    }    public static class Resources extends ListResourceBundle {        @Override        protected Object[][] getContents() { return new Object[][] {         {"greeting", "Hello"} };        }    }    public static class Resources_fr extends ListResourceBundle {        @Override        protected Object[][] getContents() { return new Object[][] {         {"greeting", "Bonjour"} };        }    }    public static void main(String[] args) {        launch(args);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5499935.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存