为什么不通过JavaFX FXML注入ImageView?

java scala javafx

1523 观看

1回复

118 作者的声誉

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>

当我运行程序时:

父root = FXMLLoader.load(getClass()。getResource(“/ fxml / Invoice.fxml”));

我得到一个例外因为imageBox2是null。知道为什么没注射吗?

作者: Adam K 的来源 发布者: 2013 年 7 月 1 日

回应 (1)


0

1 作者的声誉

SRC /
    主要/
       Main.java
       MainLayoutController.java
       main_layout.fxml
    ...

MainLayoutController.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>

Main.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(...);
    }
    ...
}
作者: Ievgen 发布者: 21.11.2018 09:06
32x32