【发布时间】:2017-03-22 12:22:14
【问题描述】:
目前,当我想将翻译包设置到特定窗格时,我必须在加载之前完成。让我们考虑一个简单的例子:
Bundle_en.properties
key=Sample Text
MainApplication.java
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
fxmlLoader.setResources(ResourceBundle.getBundle("bundles.Bundle",new Locale("en","EN")));
Parent root = fxmlLoader.load();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
现在我可以使用包内部的名称了:
sample.fxml
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<Label text="%key"/>
</GridPane>
问题:
是否可以将设置资源的过程移到控制器内部的initialize()方法中?
Controller.java
public class Controller {
@FXML
private void initialize() {
// setting resources here
}
}
【问题讨论】: