目前,当我想为特定的窗格设置翻译包时,我必须在加载之前执行此操作。让我们考虑一个简单的例子:是否可以在控制器类中设置Pane的recoures?
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
}
}