我在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
它是空的。知道为什么它没有被注射吗?