gpt4 book ai didi

JavaFX 无法设置自定义属性

转载 作者:行者123 更新时间:2023-12-02 09:41:24 24 4
gpt4 key购买 nike

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

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

标签 Controller :

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> 的属性/子项元素适用于加载其他 fxml 的结果,即在本例中创建的对象类型为 VBox ,不是LabelControllerVBox不包含您尝试分配的属性。

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

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尽管这将使 Controller 和节点成为相同的对象,从而允许您进行这样的分配。由于缺乏节点责任的信息,我保留 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>

关于JavaFX 无法设置自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57038327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com