JavaFX 将标签文本属性绑定(bind)到多个属性更改

标签 java javafx binding

我想做的是:

我有一个 JavaFX 窗口,我通过拖动角或边不断改变它的宽度和高度。我想要宽度或高度改变时然后 Label 的文本具有以下格式:

~Width=[here the width of the Window],Height[here the height of the Window]~

例子:

~Width=[1300],Height=[600]~

我想使用绑定(bind)而不是使用 2(两个)ChangeListeners 来做到这一点。

                                   I am trying:

label.textProperty().bind(I am stack here on how to do this...);

我也读过这个问题JavaFX bind to multiple properties

最佳答案

只需将 Bindings.createStringBindingwidthheight 作为依赖项一起使用:

StringBinding binding = Bindings.createStringBinding(
            () -> MessageFormat.format("~Width=[{0}],Height=[{1}]~", primaryStage.getWidth(), primaryStage.getHeight()),
            primaryStage.widthProperty(),
            primaryStage.heightProperty());

label.textProperty().bind(binding);

关于JavaFX 将标签文本属性绑定(bind)到多个属性更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41571519/

相关文章:

wpf - 如何在 WPF 中的控件上绑定(bind)本地属性

forms - 具有大型表单的 VueJS

python - 最大子图

Java Web View - native 查找

java - 错误 : JavaFX runtime components are missing - JavaFX 11 and OpenJDK 11 and Eclipse IDE

JavaFX 如何并排居中多个组件

java - 如何在Java中将8个字符的字符串转换为ARGB颜色格式?

java - 遍历 hashmap - 抛出异常

java - 4.4.4 上的 android.view.InflateException 而 5.0 则没问题

Java 无法找到或加载主类错误