"在fxml控制器类中找不到id"

时间:2017-02-21 20:53:54

标签: javafx controller fxml scenebuilder

尽管被@FXML注释,但SceneBuilder并未将控制器类中的字段注入FXML。相反,当我在Scene Builder上的fx:id中输入ID时,错误消息""在fxml控制器类中找不到id"而是显示。

我已经设法让它在过去工作,其中ID显示下拉菜单,您只需选择适当的字段;但我不确定它为什么停止工作。

FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@../../common/gui/common_style.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="vehicles.logic.addVehicle">
   <children>
      <Text fill="WHITE" layoutX="304.0" layoutY="263.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Registration Number">
         <font>
            <Font size="19.0" />
         </font></Text>
      <Text fill="WHITE" layoutX="305.0" layoutY="162.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Model">
         <font>
            <Font size="19.0" />
         </font></Text>
      <Text fill="WHITE" layoutX="307.0" layoutY="215.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Make">
         <font>
            <Font size="19.0" />
         </font></Text>
      <Text fill="WHITE" layoutX="306.0" layoutY="314.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Engine Size">
         <font>
            <Font size="19.0" />
         </font></Text>
      <Text fill="WHITE" layoutX="307.0" layoutY="363.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Fuel Type">
         <font>
            <Font size="19.0" />
         </font></Text>
      <Text fill="WHITE" layoutX="308.0" layoutY="415.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Colour">
         <font>
            <Font size="19.0" />
         </font></Text>
      <Text fill="WHITE" layoutX="306.0" layoutY="472.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MOT Renewal Date">
         <font>
            <Font size="19.0" />
         </font></Text>
      <Text fill="WHITE" layoutX="307.0" layoutY="536.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Last Service Date">
         <font>
            <Font size="19.0" />
         </font></Text>
      <Text fill="WHITE" layoutX="309.0" layoutY="597.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Current Mileage">
         <font>
            <Font size="19.0" />
         </font></Text>
      <TextField fx:id="model" layoutX="538.0" layoutY="142.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField fx:id="make" layoutX="539.0" layoutY="195.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField fx:id="regnum" layoutX="540.0" layoutY="244.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField fx:id="engine" layoutX="539.0" layoutY="295.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField fx:id="fueltype" layoutX="540.0" layoutY="344.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField fx:id="colour" layoutX="540.0" layoutY="396.0" prefHeight="25.0" prefWidth="170.0" />
      <TextField fx:id="mileage" layoutX="540.0" layoutY="577.0" prefHeight="25.0" prefWidth="170.0" />
      <DatePicker fx:id="motrenewal" layoutX="539.0" layoutY="453.0" />
      <DatePicker fx:id="servicedate" layoutX="541.0" layoutY="515.0" />
      <Button fx:id="add" layoutX="927.0" layoutY="604.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="71.0" text="Add" />
      <Button fx:id="back" layoutX="820.0" layoutY="604.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="71.0" text="Back" />
      <Text fill="WHITE" layoutX="527.0" layoutY="90.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Add Vehicle">
         <font>
            <Font size="43.0" />
         </font>
      </Text>
   </children>
</AnchorPane>

控制器:

package vehicles.logic;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;

/**
 * FXML Controller class
 *
 * @author ugonw
 */
public class addVehicle implements Initializable {

    @FXML
    private TextField model;
    @FXML
    private TextField make;
    @FXML
    private TextField regnum;
    @FXML
    private TextField engine;
    @FXML
    private TextField fueltype;
    @FXML
    private TextField colour;
    @FXML
    private TextField mileage;
    @FXML
    private DatePicker motrenewal;
    @FXML
    private DatePicker servicedate;
    @FXML
    private Button add;
    @FXML
    private Button back;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

1 个答案:

答案 0 :(得分:0)

解决方法是在IDE中打开xml文件并手动将id名称添加到该文件中。因此,如果Scenebuilder没有为您完成此操作,并且该对象涉及文本字段,那么标记将如下所示。

<TextField />

你应该像这样添加fx:id =“your_id_name”:

<TextField fx:id="userName" />

保存xml文件后它应该像魅力一样工作。希望这会有所帮助。