“什么是”呢;自动将位置和资源属性注入控制器“;在JavaFX中?

“什么是”呢;自动将位置和资源属性注入控制器“;在JavaFX中?,java,dependency-injection,annotations,javafx,Java,Dependency Injection,Annotations,Javafx,在接口说明中,说明如下: 注:该接口已被自动注入 将位置和资源属性导入控制器。FXMLLoader将 现在自动调用任何适当注释的no arg initialize() 由控制器定义的方法。建议注射 应尽可能采用这种方法 问题是:如何“适当注释”方法?我只找到一个注释--@FXML。还有其他的吗?答案是: 在JavaFX2.1及更早版本中,需要控制器类 实现可初始化的接口,当内容 已完全加载关联的FXML文档的。在JavaFX中 2.2,这已不再必要。FXMLLoader类的实例只是在控制器上查找

在接口说明中,说明如下:

注:该接口已被自动注入 将位置和资源属性导入控制器。FXMLLoader将 现在自动调用任何适当注释的no arg initialize() 由控制器定义的方法。建议注射 应尽可能采用这种方法

问题是:如何“适当注释”方法?我只找到一个注释--
@FXML
。还有其他的吗?

答案是:

在JavaFX2.1及更早版本中,需要控制器类 实现可初始化的接口,当内容 已完全加载关联的FXML文档的。在JavaFX中 2.2,这已不再必要。FXMLLoader类的实例只是在控制器上查找initialize()方法并调用 如果有,请提供。请注意,与其他FXML回调方法类似 如事件处理程序,此方法必须用@FXML注释 注释(如果不是公共的)

建议开发人员在新的应用程序中使用此方法 发展。尚未弃用可初始化接口,但 可能在将来的版本中

编辑

经过更多研究,我现在可以提供一个SSCCE,演示如何将资源束注入带有注释的控制器。请注意,本SSCCE包含对答案的轻微修改

以下是SSCCE:

com/stackexchange/stackoverflow/\u 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"));
  //}
}
<?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>
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);
  }
}
key1=Name Surname
key2=How are you?
key1=Apellido
key2=Que tal?
com/stackexchange/stackoverflow/\u 20107463/MyView.fxml:

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"));
  //}
}
<?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>
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);
  }
}
key1=Name Surname
key2=How are you?
key1=Apellido
key2=Que tal?
com/stackexchange/stackoverflow/\u 20107463/MyBundle。属性:

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"));
  //}
}
<?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>
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);
  }
}
key1=Name Surname
key2=How are you?
key1=Apellido
key2=Que tal?

suzan@fxml标记显示变量或控制器是用fxml设计的……为了设计更好的fxml方式,这可能会帮助您
stackoverflow.com/questions/19523341/adding-a-tilepane-instantified-in-java-files-to-fxml
我很惊讶答案还没有被接受