繁体   English   中英

JavaFX Bind Label textProperty更改为多个属性

[英]JavaFX Bind Label textProperty to multiple Properties changes

我想做的是:

我有一个JavaFX窗口,我通过拖动corners or the sides不断更改其宽度和高度。 I want when the width or height are changing时, Label的文本具有以下格式:

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

例:

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

我想使用绑定而不是2( ChangeListenersChangeListeners来做到这一点

                                   I am trying:

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

我也读过这个问题JavaFX绑定到多个属性

只需将Bindings.createStringBindingwidthheight作为依赖项:

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

label.textProperty().bind(binding);

我认为最简单的方法是使用更改侦听器,并手动查询with / height值。 喜欢

public void init()
{
     ChangeListener<Number> listener = (obs, ov, nv) -> update();
    node.widthProperty().addListener(listener);
    node.heightProperty().addListener(listener);
}

public void update()
{
      label.setText(String.format("width[%s] height[%s]", node.getWidth()), node.getHeight);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM