2013-11-20 49 views
7

Initializable接口的描述據說:JavaFX中的「自動將位置和資源屬性注入控制器」是什麼?

注意此接口已經通過 位置和資源屬性到控制器自動注射取代。 FXMLLoader將自動調用由控制器定義的任何適當註釋的no-arg初始化() 方法。建議儘可能使用注入方法 。

問題是:如何「適合註釋」的方法?我只找到一個註釋 - @FXML。還有其他人嗎?

+0

蘇珊@fxml標籤顯示,該變量或控制器在FXML設計...設計更好FXML方式,可能是這個幫助你'stackoverflow.com/questions/19523341/adding-a-tilepane -instantiated-in-java-files-to-fxml' –

+0

我很驚訝,答案還沒有被接受。 :) – axiopisty

回答

15

答案是located here

在JavaFX 2.1和更早的版本,控制器類被要求 實現Initializable接口時,內容相關FXML文件 已經完全加載通知。在JavaFX 2.2中,這不再是必需的。如果可用,FXMLLoader類的一個實例只需在控制器上查找initialize()方法並調用 即可。請注意,與其他FXML回調方法(如事件處理程序)類似,如果該方法未公開,則必須使用@FXML 註釋進行註釋。

建議開發人員使用此方法開發新的 。 Initializable接口尚未被棄用,但 可能在未來版本中。

編輯

更多的研究後,我可以立刻提供SSCCE展示如何注入一個資源包與註釋的控制器。請注意,此SSCCE包含對this SO question的答案的輕微修改。

這裏是SSCCE:

COM/stackexchange /計算器/ _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/stackexchange /計算器/ _20107463/MyView.fxml:

<?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/stackexchange /計算器/ _20107463/MyBundle_en.properties:

key1=Name Surname 
key2=How are you? 

COM/stackexchange /計算器/ _20107463/MyBundle_es。屬性:

key1=Apellido 
key2=Que tal?