简体   繁体   English

JavaFx fx:id 导致错误

[英]JavaFx fx:id causes error

For some reason, my fx:id does not bind properly to my Controller class, and thus always causes an error.出于某种原因,我的 fx:id 没有正确绑定到我的Controller类,因此总是会导致错误。

Controller控制器

package sample;

import javafx.fxml.FXML;

import java.awt.*;
import java.awt.event.ActionEvent;

public class Controller {

    @FXML public Button button;

    public void clickAction(ActionEvent actionEvent) {
        System.out.println("Button clicked.");
    }
}

FXML文件格式

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <Button text="Click me!" fx:id="button" onAction="#clickAction" BorderPane.alignment="CENTER"/>
</BorderPane>

I think I understand the source of my problem, but I do not understand how to properly address it.我想我明白我的问题的根源,但我不明白如何正确解决它。 According an answer of this question , I think I am trying to assign FXML elements before the constructor is called (and these elements can only be assigned during/after initialisation).根据这个问题的答案,我想我试图在调用构造函数之前分配 FXML 元素(并且这些元素只能在初始化期间/之后分配)。

Is there a way to do this without implementing Initializable ?有没有办法在实现Initializable情况下做到这一点? Or am I making a completely different mistake?还是我犯了一个完全不同的错误?

You use the imports你使用进口

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

in your fxml file.在您的 fxml 文件中。

Therefore the Button instance created while loading the fxml file is a javafx.scene.control.Button .因此,加载 fxml 文件时创建的Button实例是javafx.scene.control.Button

The type of the field this is injected to needs be something a javafx.scene.control.Button can be assigned to.注入的字段类型需要是javafx.scene.control.Button可以分配的类型。

Since your only imports in the controller besides javafx.fxml.FXML are from the java.awt packages, this is clearly not the case for the button field (type java.awt.Button ).由于除了javafx.fxml.FXML之外,您在控制器中的唯一导入来自java.awt包,因此button字段(类型java.awt.Button )显然不是这种情况。

Fix your controller to import the required classes from the javafx packages instead:修复您的控制器以从javafx包中导入所需的类:

import javafx.fxml.FXML;

import javafx.scene.control.Button;
import javafx.event.ActionEvent;

BTW: You can also leave out the parameter of the onAction handler, if you do not use it:顺便说一句:如果您不使用它,您也可以省略onAction处理程序的参数:

public void clickAction() {
    System.out.println("Button clicked.");
}

Note that:注意:

1)You were using the old Swing library in the import statements 1)您在导入语句中使用了旧的Swing

2)You need to add @FXML on every method and element with id defined using fxml 2)您需要在每个方法和元素上添加@FXML ,其id使用fxml定义

Your code should be:你的代码应该是:

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;

public class Controller {

    @FXML
     public Button button;

     @FXML
    public void clickAction(ActionEvent actionEvent) {
        System.out.println("Button clicked.");
    }
}

Also mention that as Fabian and Michael said on their answers you don't need ActionEvent actionEvent into the clickAction method.So finnally:还要提到,正如FabianMichael在他们的回答中所说的,你不需要 ActionEvent actionEvent 到 clickAction 方法中。所以最后:

 @FXML
        public void clickAction() {
            System.out.println("Button clicked.");
        }

The only problem I see if your import of ActionEvent.我看到的唯一问题是您导入的 ActionEvent。 You are using awt and should be using javafx ActionEvent.您正在使用 awt 并且应该使用 javafx ActionEvent。

import javafx.event.ActionEvent

Edit 1编辑 1

You also dont need to have ActionEvent as a parameter if you dont need it, just an FYI.如果您不需要它,您也不需要将 ActionEvent 作为参数,仅供参考。

Recently, I think I had a similar problem.最近,我想我遇到了类似的问题。 FXML was giving me error. FXML 给了我错误。 The reason was an incorrectly imported library.原因是导入的库不正确。 Instead java.scene.control , I imported java.awt .而不是java.scene.control ,我导入了java.awt

look at the possibilities:看看可能性:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM