将IntegerProperty [value:]更改为公正值

问题描述 投票:0回答:1

我绑定了工厂中剩余物品的数量。

private int item;
private SimpleIntegerProperty itemProperty = new SimpleIntegerProperty();
private SimpleIntegerProperty remainingItem = new Simple IntegerProperty();
private SimpleIntegerProperty soldItem = new Simple IntegerProperty();

在构造函数中

this.item = item;
itemProperty.set(item);
soldItem.set(0);
remainingItem.bind(soldItem.subtract(item).multiply(-1));
//itemProperty.bind((soldItem.subtract(item).multiply(-1));

在fxml文件中

<Text text="${controller.factory.remainingProperty.get()}">

[当我尝试绑定itemProperty时,它给出了无效的当我尝试使用get方法显示剩余属性ReadOnlyIntegerProperty时,它将在主类中引发EXCEPTION

java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException: 
/Users/sailaunghein/Desktop/storemvclab/out/production/storemvclab/store.fxml:20

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at StoreApplication.start(StoreApplication.java:12)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.IllegalArgumentException: Unexpected character in expression.
    at javafx.fxml/com.sun.javafx.fxml.expression.Expression$Parser.tokenize(Expression.java:411)
    at javafx.fxml/com.sun.javafx.fxml.expression.Expression$Parser.parse(Expression.java:80)
    at javafx.fxml/com.sun.javafx.fxml.expression.Expression.valueOf(Expression.java:1503)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:318)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:242)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:775)
    at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
    ... 9 more

我正在尝试从item中减去项目数并显示它。

java javafx fxml
1个回答
0
投票

Expression bindings不是方法调用;他们有不同的语法。在此,假设已在控制器中定义了适当的方法(factory)并具有适当的remainingItemProperty()属性,则应使用

<Text text="${controller.factory.remainingItem}">
© www.soinside.com 2019 - 2024. All rights reserved.