Java 在 FXML 控制器类中找不到可注入的字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26865596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No injectable field found in FXML Controller class
提问by rzaaeeff
It's about JavaFX. When i want to inject fx:id in Scene Builder, i get this warning:
No injectable field found in FXML Controller class for the id 'something'.
I wanted to ignore it, and created a function, but it didn't work either. I created mainController class and added it into my FXML file. Here are my codes...
mainController.java
这是关于JavaFX。当我想在 Scene Builder 中注入 fx:id 时,我收到此警告:
在 FXML 控制器类中找不到 ID 'something' 的可注入字段。
我想忽略它,并创建了一个函数,但它也不起作用。我创建了 mainController 类并将其添加到我的 FXML 文件中。这是我的代码...
mainController.java
package main;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Slider;
public class mainController implements Initializable {
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
@FXML
private ProgressBar pb;
@FXML
private Slider sl;
@FXML
private Label label;
public void changed(ActionEvent event){
}
}
Main.java
主程序
package main;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/fxml/main.fxml"));
Scene scene = new Scene(root, 600, 400);
scene.getStylesheets().add("/fxml/styles/main.css");
ProgressBar pb1 = new ProgressBar();
ProgressBar pb2 = new ProgressBar();
//pb1.
//primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.setTitle("Something");
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
main.fxml
主文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="main.mainController">
<children>
<BorderPane layoutX="14.0" layoutY="14.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<ProgressBar fx:id="pb" prefWidth="200.0" progress="0.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
</BorderPane.margin>
</ProgressBar>
</top>
<bottom>
<Slider fx:id="sl" onDragDetected="#changed" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
</BorderPane.margin>
</Slider>
</bottom>
</BorderPane>
</children>
</AnchorPane>
I did same things in my old projects, and they work like a charm. But this one seems not to obey me. Thanks in advance...
我在我的旧项目中做了同样的事情,它们就像一个魅力。但这个人似乎不听我的话。提前致谢...
采纳答案by José Pereda
In Scene Builder, if the FXML file is associated to a controller class, you know that what makes the connection between the variable in the controller class (pb
) and the object in the FXML file (<ProgressBar ... />
) is the value of the object's fx:id
.
在 Scene Builder 中,如果 FXML 文件与控制器类相关联,您就会知道,控制器类中的变量 ( pb
) 与 FXML 文件<ProgressBar ... />
中的对象( ) 之间的联系是对象的fx:id
.
So when you set an fx:id
on an object, Scene Builder tries to parse the controller class trying to find a variable of that name.
因此,当您fx:id
在对象上设置 an时,Scene Builder 会尝试解析控制器类,尝试查找该名称的变量。
If it doesn't find any, it displays this warning. It's just a reminder that you may want to add such a variable to the controller class, or that you have to choose other valid name from a list.
如果没有找到,它会显示此警告。这只是提醒您可能希望将此类变量添加到控制器类,或者您必须从列表中选择其他有效名称。
Since you have label
defined on your controller, if you try to add a Label
on Scene Builder, you can get its fx:id
from the list:
由于您已label
在控制器上定义,如果您尝试Label
在 Scene Builder 上添加一个,您可以fx:id
从列表中获取它:
But if you assing another name, you will get the warning:
但是如果你指定另一个名字,你会收到警告:
On a side note, you don't need to instantiate the progress bar on the main class, since it will be instantiated in the controller. And ìf you try to link change(ActionEvent event)
to a method in Scene Builder (#change
), you have to annotate it with @FXML
. Anyway, don't use onDragDetected
with the slider.
附带说明一下,您不需要在主类上实例化进度条,因为它将在控制器中实例化。并且如果您尝试链接change(ActionEvent event)
到 Scene Builder ( #change
) 中的方法,则必须使用@FXML
. 无论如何,不要onDragDetected
与滑块一起使用。
回答by Mike Dever
don't forget that Netbeans will Auto-Generate the code in the controller class.
不要忘记 Netbeans 将自动生成控制器类中的代码。
Goto your FXMLDocument.fxml in your Netbeans project, and right click and select: "Make Controller".
转到 Netbeans 项目中的 FXMLDocument.fxml,然后右键单击并选择:“Make Controller”。
Also, remember to save your changes first to your FXMLDocument.fxml, in Scene builder.
另外,请记住首先将更改保存到场景构建器中的 FXMLDocument.fxml。