JavaFX 无法设置自定义属性

JavaFX Unable To Set Custom Properties

我正在尝试将自定义属性实现到我的 JavaFX 应用程序的自定义组件中。我已经阅读了一些教程,所有教程都为我指明了以下方向。

由于某种原因,它不起作用。当我尝试在 FXML 文件中设置 属性 值并且场景构建器也不显示 属性 时,IntelliJ 不喜欢。

标签控制器:

public class LabelController {

    @FXML
    public Label label;

    // Define a variable to store the property
    private DoubleProperty amountDue = new SimpleDoubleProperty();

    // Define a getter for the property's value
    public final double getAmountDue(){return amountDue.get();}

    // Define a setter for the property's value
    public final void setAmountDue(double value){amountDue.set(value);}

    // Define a getter for the property itself
    public DoubleProperty amountDueProperty() {return amountDue;}

    public void onMouseEntered(MouseEvent mouseEvent) {
        FadeToHoverColour();
    }

    public void onMouseExited(MouseEvent mouseEvent) {
        FadeToDefaultColour();
    }

    public void FadeToHoverColour() {
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(0.2), new KeyValue(label.textFillProperty(), Paint.valueOf("E63700"))));
        timeline.play();
    }

    public void FadeToDefaultColour() {
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(0.2), new KeyValue(label.textFillProperty(), Paint.valueOf("FF774D"))));
        timeline.play();
    }
}

Label.fxml:

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>

<VBox xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      fx:controller="madoc.controllers.components.LabelController">
    <Label fx:id="label"
           text="TEXT"
           onMouseEntered="#onMouseEntered"
           onMouseExited="#onMouseExited">
    </Label>
</VBox>

WelcomeSceneBuilder.fxml:

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

<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="madoc.controllers.scenes.WelcomeSceneController">
    <fx:include source="./Label.fxml" [NOT WORKING WHEN I TRY TO SET AMOUNT DUE HERE]/>
</AnchorPane>

添加到<fx:include>元素的attributes/children适用于加载其他fxml的结果,即在这种情况下创建的对象类型是VBox,而不是[=17] =]. VBox 不包含您要分配的属性。

您不能仅使用 fxml 来执行此操作。您需要使用控制器的 initialize 方法来设置 属性 值:

WelcomeSceneController

@FXML
private LabelController labelController;

@FXML
private void initialize() {
    labelController.setAmountDue(...);
}

WelcomeSceneBuilder.fxml

...
<fx:include source="./Label.fxml" fx:id="label"/>
...

您可以使用 the Custom Component approach,尽管这会使控制器和节点成为同一对象,从而允许您进行此类分配。由于缺乏对节点职责的了解,我保留 LabelController,当然你应该选择一个更好的名称。

package madoc.controllers.components;

...

public class LabelController extends VBox {

    public LabelController() {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/madoc/controllers/components/Label.fxml")); // TODO: replace with correct resoure path?
        loader.setRoot(this);
        loader.setController(this);
        try {
            loader.load();
        } catch(IOException ex) {
            throw new IllegalStateException("Could not load fxml file", ex);
        }
    }

    @FXML
    public Label label;

    // Define a variable to store the property
    private final DoubleProperty amountDue = new SimpleDoubleProperty();

    // Define a getter for the property's value
    public final double getAmountDue(){return amountDue.get();}

    // Define a setter for the property's value
    public final void setAmountDue(double value){amountDue.set(value);}

    // Define a getter for the property itself
    public DoubleProperty amountDueProperty() {return amountDue;}

    ...
}

Label.fxml

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>

<fx:root xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      type="javafx.scene.layout.VBox">
    <Label fx:id="label"
           text="TEXT"
           onMouseEntered="#onMouseEntered"
           onMouseExited="#onMouseExited">
    </Label>
</fx:root>

WelcomeSceneBuilder.fxml

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

<?import javafx.scene.layout.AnchorPane?>
<?import madoc.controllers.components.LabelController?>

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="madoc.controllers.scenes.WelcomeSceneController">
    <LabelController amountDue="30.05"/>
</AnchorPane>