有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JavaFx:使用加法/乘法双向绑定数字/

我想绑定两个DoubleProperty,但实际上不是1:1,而是以1:1.2的比例绑定

我需要像这样的东西:

DoubleProperty x;
DoubleProperty y;

x.bindBidirectional(y.multiply(1.2));

因此,每次设置x的值时,y的值应该是x*1.2 每次设置y值时,x应为y/1.2

我怎么做


共 (1) 个答案

  1. # 1 楼答案

    好吧,现在还没有什么东西存在,所以你需要自己去做一些类似的事情

    public abstract class BidirectionalBinding<S, T> {
    
        protected final Property<S> property1;
        protected final Property<T> property2;
    
        protected boolean calculating = false;
        private final InvalidationListener listener;
    
        /**
         * Convert value for property 1 to value for property 2
         * 
         * @param value
         * @return
         */
        protected abstract T convert(S value);
    
        /**
         * Convert value for property 2 to value for property 1
         * 
         * @param value
         * @return
         */
        protected abstract S inverseConvert(T value);
    
        protected BidirectionalBinding(Property<S> property1, Property<T> property2) {
            if (property2.isBound() || property1 == null) {
                throw new IllegalArgumentException();
            }
    
            this.property1 = property1;
            this.property2 = property2;
            property2.setValue(convert(property1.getValue()));
    
            listener = o -> {
                if (!calculating) {
                    calculating = true;
                    try {
                        if (o == property1) {
                            T value = convert(property1.getValue());
                            property2.setValue(value);
                        } else {
                            S value = inverseConvert(property2.getValue());
                            property1.setValue(value);
                        }
                    } catch (Exception ex) {
                        dispose();
                    }
                    calculating = false;
                }
            };
    
            property1.addListener(listener);
            property2.addListener(listener);
        }
    
        public void dispose() {
            property1.removeListener(listener);
            property2.removeListener(listener);
        }
    }
    
    DoubleProperty x = new SimpleDoubleProperty(3);
    DoubleProperty y = new SimpleDoubleProperty();
    final double factor = 1.2;
    
    BidirectionalBinding<Number, Number> binding = new BidirectionalBinding<>(x, y) {
    
        @Override
        protected Number convert(Number value) {
            return value.doubleValue() * factor;
        }
    
        @Override
        protected Number inverseConvert(Number value) {
            return value.doubleValue() / factor;
        }
    
    };
    
    System.out.printf("x = %f; y = %f\n", x.get(), y.get());
    x.set(5);
    System.out.printf("x = %f; y = %f\n", x.get(), y.get());
    y.set(15);
    System.out.printf("x = %f; y = %f\n", x.get(), y.get());
    

    请注意,此实现是通用的。如果您处理的是专门的属性,您可能希望修改代码以使用基本类型,以避免转换为包装类型