cevap located here geçerli: içindekiler ilişkili FXML belgenin tamamen dolu olmuştu zaman
JavaFX 2.1
ve önceki, kontrolör sınıfları Sıfırlanabilir arabirimini uygulamak istendi bildirilmesini. JavaFX 2.2'de, artık gerekli değildir. FXMLLoader sınıfının bir örneği, denetleyicideki initialize() yöntemini arar ve varsa 'u çağırır. Olay işleyicileri gibi diğer FXML geri çağırma yöntemleri gibi, bu yöntemin, public değilse, @FXML ek açıklamasıyla açıklanması gerektiğini unutmayın.
Geliştiricilerin bu yaklaşımı yeni gelişimi için kullanmaları önerilir. Başlatılabilir arabirim kullanımdan kaldırıldı, ancak ileride yayınlanacak bir sürümde olabilir.
DÜZENLEME
fazla araştırma sonra şimdi ek açıklamaları ile bir kontrol bir kaynak paketi enjekte nasıl ortaya koyan bir SSCCE sağlayabilir. Lütfen bu SSCCE'nin this SO question cevabından küçük değişiklikler içerdiğini unutmayın.
com/Stack Exchange/stackoverflow/_20107463/MyController.java:
package com.stackexchange.stackoverflow._20107463;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class MyController {
@FXML
private Label label;
@FXML private ResourceBundle resources;
@FXML
private void initialize() {
label.setText(resources.getString("key1"));
}
// Or if you don't want to use @FXML you could do:
//public void initialize() {
// label.setText(resources.getString("key1"));
//}
}
com/Stack Exchange/stackoverflow/_20107463/MyView.fxml:
İşte
SSCCE olduğunu
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.*?>
<BorderPane fx:controller="com.stackexchange.stackoverflow._20107463.MyController" xmlns:fx="http://javafx.com/fxml">
<top>
<!-- This label's text will be set by the controller -->
<Label fx:id="label"/>
</top>
<center>
<!-- This label's text will be taken from the bundle automatically -->
<Label text="%key2"/>
</center>
</BorderPane>
com/stackexchange/stackoverflow/_20107 463/BundleDemo.java:
package com.stackexchange.stackoverflow._20107463;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class BundleDemo extends Application {
private Stage stage;
@Override
public void start(Stage primaryStage) {
stage = primaryStage;
Button btnEN = new Button();
btnEN.setText("English");
btnEN.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
loadView(new Locale("en", "EN"));
}
});
Button btnKG = new Button();
btnKG.setText("Español");
btnKG.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
loadView(new Locale("es", "ES"));
}
});
VBox root = new VBox(20);
root.getChildren().add(HBoxBuilder.create().spacing(10).style("-fx-background-color: gray").padding(new Insets(5)).children(btnEN, btnKG).build());
root.getChildren().add(new StackPane());
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private void loadView(Locale locale) {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setResources(ResourceBundle.getBundle("com.stackexchange.stackoverflow.bundles.MyBundle", locale));
Pane pane = (BorderPane) fxmlLoader.load(this.getClass().getResource("MyView.fxml").openStream());
// replace the content
StackPane content = (StackPane) ((VBox) stage.getScene().getRoot()).getChildren().get(1);
content.getChildren().clear();
content.getChildren().add(pane);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
com/Stack Exchange/stackoverflow/_20107463/MyBundle_en.properties:
key1=Name Surname
key2=How are you?
com/Stack Exchange/stackoverflow/_20107463/MyBundle_es.Özellikleri:
key1=Apellido
key2=Que tal?
suzan @fxml etiketi, bu yardım 'stackoverflow.com/questions/19523341/adding-a-tilepane olabilir daha iyi fxml yolu tasarlamak için değişken veya kontrolör ... fxml tasarım olduğunu göstermektedir -instantiated-in-java-files-to-fxml ' –
Cevabın henüz kabul edilmediğine şaşırdım. :) – axiopisty