我只是试图绑定一个 Integer 和一个 String 属性。经过一番谷歌搜索后,这应该可以使用提供的两种方法之一来实现:
公共静态无效绑定双向(属性stringProperty,
属性otherProperty,StringConverter转换器)public static void bindBidirectional(Property stringProperty,
Property otherProperty, java.text.Format 格式)
不幸的是,这似乎对我不起作用。我究竟做错了什么?
import java.text.Format;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.util.converter.IntegerStringConverter;
public class BiderectionalBinding {
public static void main(String[] args) {
SimpleIntegerProperty intProp = new SimpleIntegerProperty();
SimpleStringProperty textProp = new SimpleStringProperty();
Bindings.bindBidirectional(textProp, intProp, new IntegerStringConverter());
intProp.set(2);
System.out.println(textProp);
textProp.set("8");
System.out.println(intProp);
}
}