【发布时间】:2017-05-09 07:09:22
【问题描述】:
这是我的StartApp.java,我的应用程序的入口点。
public class StartApp extends Application {
private Locale locale = new Locale("en");
public Locale getLocale(){
return locale;
}
public void setLocale(Locale locale){
this.locale = locale;
}
@Override
public void start(Stage primaryStage) throws Exception{
ResourceBundle bundle = ResourceBundle.getBundle("resources.bundles/MyBundle", locale);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/LoginView.fxml"), bundle);
Parent root = fxmlLoader.load();
primaryStage.setTitle("Title");
primaryStage.setScene(new Scene(root, 750, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) throws SQLException {
launch(args);
}
然后在 LoginController.java 创建 StartApp 实例并为 2 个按钮设置 onActions
StartApp startApp = new StartApp();
@Override
public void initialize(URL location, ResourceBundle resources) {
bundle = resources;
plBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
startApp.setLocale(new Locale("pl"));
changeLanguage(event);
} catch (Exception e) {
e.printStackTrace();
}
}
});
enBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
startApp.setLocale(new Locale("en"));
changeLanguage(event);
} catch (Exception e) {
e.printStackTrace();
}
}
});
这是我的changeLanguage 方法,它刷新当前窗口并更改其语言
public void changeLanguage(ActionEvent event) throws Exception{
((Node)event.getSource()).getScene().getWindow().hide();
Stage primaryStage = new Stage();
ResourceBundle bundle = ResourceBundle.getBundle("resources.bundles/MyBundle", startApp.getLocale());
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/LoginView.fxml"), bundle);
Parent root = fxmlLoader.load();
primaryStage.setTitle("Title2");
primaryStage.setScene(new Scene(root, 750, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
到目前为止,一切正常,一旦我单击按钮,它就会改变语言。但我现在想做的是用选择的语言打开新窗口(舞台),但不幸的是,它总是用 StartApp 上设置的语言打开新场景。
这是LoginController 中的方法,而不是打开新阶段。
public void register(ActionEvent event) throws Exception{
((Node)event.getSource()).getScene().getWindow().hide();
Stage primaryStage = new Stage();
ResourceBundle bundle = ResourceBundle.getBundle("resources.bundles/MyBundle", startApp.getLocale());
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/RegisterView.fxml"), bundle);
Parent root = fxmlLoader.load();
primaryStage.setTitle("Title2");
primaryStage.setScene(new Scene(root, 750, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
顺便说一句。 Iv 尝试将 StartApp 扩展到 LoginController,将语言环境公开等,每次结果都相同。当我创建
Locale newLocale = null;
在LoginController 中,然后在单击initialize 中定义的按钮后尝试为其分配值,但出现空指针异常。
【问题讨论】:
-
当你重新加载
LoginView.fxml时,它会创建一个新的控制器;在该控制器中,您创建StartApp的新实例,并且您没有在该实例上设置语言环境。这里的方法似乎是错误的:创建您自己的Application子类实例基本上总是一个坏主意:应该只有一个实例(为您创建的实例,在其上调用start(...))。尝试为此使用 MVC 方法,并将语言环境(或者可能是资源包)作为属性包含在模型中。然后只需与所有控制器共享一个模型实例。
标签: javafx locale resourcebundle