3

我在ImageView通过 JavaFX 中的 FXMl 注入组件时遇到问题。我有一个用 Scala 编写的控制器,如下所示:

package me.mycontroller
import javafx.fxml.FXML
import java.util.ResourceBundle
import java.net.URL
import javafx.scene.image.ImageView

/** 
 * @author me
 */
 class InvoiceController {

    @FXML
    private var resources: ResourceBundle = null

    @FXML
    private var location: URL = null


    @FXML
    private var imageBox2: ImageView = null


    @FXML
    def initialize() {
        if(imageBox2==null) { throw new IllegalArgumentException("imageBox was null")}



    }


}

我的FXMl是这样的:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"     minWidth="-Infinity" prefHeight="286.0" prefWidth="512.0" xmlns:fx="http://javafx.com/fxml" fx:controller="me.mycontroller.InvoiceController">
    <children>
        <HBox alignment="CENTER" prefHeight="100.0" prefWidth="435.0" spacing="10.0" AnchorPane.bottomAnchor="58.0" AnchorPane.leftAnchor="38.5" AnchorPane.rightAnchor="38.5" AnchorPane.topAnchor="128.0">
          <children>
            <Label text="Invoice File" />
            <TextField fx:id="fileField" prefWidth="200.0" />
          </children>
        </HBox>
        <HBox alignment="CENTER" prefHeight="100.0" prefWidth="400.5" spacing="10.0" AnchorPane.bottomAnchor="-7.0" AnchorPane.leftAnchor="52.5" AnchorPane.rightAnchor="59.0" AnchorPane.topAnchor="193.0">
          <children>
            <AnchorPane prefHeight="100.0" prefWidth="376.0">
              <children>
            <AnchorPane layoutX="16.0" layoutY="0.0" prefHeight="100.0" prefWidth="294.0">
              <children>
                <Label layoutY="42.0" prefWidth="65.0" text="Supplier" AnchorPane.leftAnchor="25.0" />
                <ChoiceBox fx:id="supplierDropDown" layoutY="40.0" prefWidth="200.0" AnchorPane.rightAnchor="5.0">
                  <items>
                    <FXCollections fx:factory="observableArrayList">
                      <String fx:value="Item 1" />
                      <String fx:value="Item 2" />
                      <String fx:value="Item 3" />
                    </FXCollections>
                  </items>
                </ChoiceBox>
              </children>
            </AnchorPane>
          </children>
        </AnchorPane>
      </children>
    </HBox>
    <ImageView fx:id="imageBox2" fitHeight="105.99999961206467" fitWidth="141.3333282470703" layoutX="53.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true">
      <image>
        <Image url="@../images/Regional.jpeg" />
      </image>
    </ImageView>
  </children>
</AnchorPane>

当我像这样运行程序时:

父根 = FXMLLoader.load(getClass().getResource("/fxml/Invoice.fxml"));

我得到一个例外,因为imageBox2它是空的。知道为什么它没有被注射吗?

4

1 回答 1

0
源/
    主要的/
       主.java
       MainLayoutController.java
       main_layout.fxml
    ...

主布局控制器.java

public class MainLayoutController {

@FXML
private ImageView previewImage;

void setImage(Image img) {
    previewImage.setImage(img);
}

}

main_layout.fxml

<VBox ... fx:controller="main.MainLayoutController">
    <children>
       ...
       <ImageView fx:id="previewImage" ... />
    </children>
</VBox>

主.java

public class Main extends Application {

    private MainLayoutController mainLayoutController;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader();
        Parent root = fxmlLoader.load(
            getClass().getResource("main_layout.fxml").openStream());

        primaryStage.setScene(new Scene(root));
        primaryStage.show();

        mainLayoutController = fxmlLoader.getController();
        mainLayoutController.setImage(...);
    }
    ...
}
于 2018-11-21T09:06:49.917 回答