【发布时间】:2017-04-13 06:25:18
【问题描述】:
我正在尝试创建一个内部包含图像部分的文本字段,类似于this。
imageView.fitHeightProperty().bind(Bindings.createDoubleBinding(
() -> textField.getHeight() -
textField.getPadding().getTop() -
textField.getPadding().getBottom(),
textField.heightProperty(), textField.paddingProperty()));
imageView.fitWidthProperty().bind(imageView.fitHeightProperty());
textField.paddingProperty().bind(Bindings.createObjectBinding(
() -> new Insets(textField.getPadding().getTop(),
textField.getPadding().getRight(),
textField.getPadding().getBottom(),
textField.getPadding().getRight() * 2 + imageView.getFitWidth()),
imageView.fitWidthProperty(), textField.paddingProperty()));
我目前的方法是使用StackPane 来保存TextField,然后还添加一个ImageView 作为StackPane 的孩子。 ImageView 需要知道如何调整自身大小,因此我将其 fitHeight 绑定到 TextField 的高度,同时考虑到 TextField 的填充。
ImageView 的fitWidth 然后绑定到它自己的fitHeight。
最后,我需要让我的TextField 的文本向右偏移(因为图像挡住了它),所以我再次做了另一个依赖于ImageView 的fitWidth 的绑定。
这会导致循环依赖,导致堆栈溢出。有没有其他方法可以在不对TextField 的左填充进行硬编码的情况下做到这一点?
【问题讨论】:
-
为什么需要调整图片大小?
标签: java user-interface javafx data-binding javafx-8