将JavaFx标签绑定到根据三个节点属性更改的函数输出

时间:2019-02-26 06:07:00

标签: javafx javafx-8

我想制作一个标签textProperty绑定到一个函数的值,该函数将获取一个Nodes数组(即三个Line Nodes),然后根据Nodes的position属性(即startXProperty)进行一些计算,因此只要更改节点的位置,标签文本就会相应更新。

这是我的尝试:

Label label = new Label();

DoubleProperty myFunction(Line[] lines){

      DoubleProperty property= new SimpleDoubleProperty();

      // This is a sample computation, because the actual computation is much more complex. 
      // That's why I tried to avoid using the arithmetic methods provided by the Property class,
      // i.e., property().add().multiply()

      double computation = Math.sqrt(Math.pow(lines[0].startXProperty().getValue() - lines[1].startXProperty().getValue());

      property.setValue(computation);

      return property;
}

label.textProperty().bind(myFunction(lines).asString());

这种方法行不通。我正在寻找解决此问题的方法。谢谢!

1 个答案:

答案 0 :(得分:1)

更新:已解决

由于评论中提供了答案,我将函数更改为返回<?php /** * The main template file. */ get_header(); ?> <main id="main" class="site-main" role="main"> <section class="content-area full-layout"> <?php the_archive_title( '<h1 class="section-title">', '</h1>' ); ?> <?php if (is_category() ) { ?><div class="category_description"><?php echo category_description(); ?></div><?php } ?> <?php if ( have_posts() ) : ?> <section id="recent-posts" class="recent-posts"> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> </section> <?php get_template_part( 'pagination' ); ?> <?php else: ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?> <div class="clear"></div> </section><!-- .content-area --> </main><!-- .site-main --> <?php get_footer(); 并将DoubleBinding绑定到它,然后它起作用了!

label

它现在按预期运行!最后一个问题,在使用Label label = new Label(); DoubleBinding myFunction(Line[] lines){ DoubleProperty line_StartX[] = new DoubleProperty[lines.length]; DoubleProperty line_EndX[] = new DoubleProperty[lines.length]; DoubleProperty line_StartY[] = new DoubleProperty[lines.length]; DoubleProperty line_EndY[] = new DoubleProperty[lines.length]; for (int i = 0; i < lines.length; i++) { line_StartX[i] = lines[i].startXProperty(); line_EndX[i] = lines[i].endXProperty(); line_StartY[i] = lines[i].startYProperty(); line_EndY[i] = lines[i].endYProperty(); } DoubleBinding distBinding = new DoubleBinding() { { for (int i=0; i<3; i++){ super.bind(line_StartX[i]); super.bind(line_EndX[i]); super.bind(line_StartY[i]); super.bind(line_EndY[i]); } } @Override protected double computeValue() { double a = Math.sqrt(Math.pow(lines_StartX[0].getValue() - lines_StartX[1].getValue(),2)); return a; } }; return distBinding; } label.textProperty().bind(myFunction(lines).asString()); 时,有没有更简单的方法可以一次在数组中添加整堆元素?