在anchorPane

时间:2019-04-15 16:03:24

标签: java javafx

我的任务是在全屏模式下创建可调整大小的图形。默认情况下,当用户更改全屏程序窗口时,图形必须可调整大小(图形的组件和行更改其大小)。我用AnchorPane来实现图:GridPane元素位于定义的坐标中。然后,借助方法getBoundsInParent()进行划线。这是图形的scema:

image

一切都很好,但是问题是我无法调整图形的大小。所有组件都保持其大小;变量prefSizeminSizemaxSize不调整大小。我尝试使用参数AnchorPane.setTopAnchor等,但是它们不调整大小,仅移动GridPane组件。 另外,我尝试使用GridPane作为布局而不是AnchorPane。但是,与方法component.getBoundsInParent()绑定的行以随机位置飞走(我知道getBoundsInParent()方法返回的其他坐标是GridPane)。

我的项目位于没有Internet的办公计算机上,我无法显示它。我认为绑定图形之间的线的方式在代码块中显示很有用,因为当组件处于GridPane布局中时,这是导致线移出的原因:

line.startXProperty().bind(source.layoutXProperty().add(source.getBoundsInParent().getWidth() / 2.0));    
line.startYProperty().bind(source.layoutYProperty().add(source.getBoundsInParent().getHeight() / 2.0));
line.endXProperty().bind(target.layoutXProperty().add(target.getBoundsInParent().getWidth() / 2.0));
line.endYProperty().bind(target.layoutYProperty().add(target.getBoundsInParent().getHeight() / 2.0));

使用我创建的元素以及与该元素连接的线来调整图形大小的方法是什么。可能是AnchorPaneGridPane的属性吗?还是一些行的起点和终点的绑定?

1 个答案:

答案 0 :(得分:0)

绑定必须基于属性或其他ObservableValue实现,因此可以正确跟踪对其值的更改。在代码创建绑定的那一刻,像source.getBoundsInParent().getWidth() / 2.0这样的直接方法调用仅被评估一次,因此宽度的变化永远不会被看到。

line.startXProperty().bind(
    source.layoutXProperty().add(
        source.widthProperty().divide(2)));
line.startYProperty().bind(
    source.layoutYProperty().add(
        source.heightProperty().divide(2)));

如果sourcetarget不是区域,因此没有width属性,则可以使用Bindings创建其边界的动态绑定:< / p>

DoubleBinding width = Bindings.createDoubleBinding(
    () -> source.getLayoutBounds().getWidth(), 
    source.layoutBoundsProperty());

line.startXProperty().bind(
    source.layoutXProperty().add(
        Bindings.createDoubleBinding(width.divide(2)));

DoubleBinding height = Bindings.createDoubleBinding(
    () -> source.getLayoutBounds().getHeight(), 
    source.layoutBoundsProperty());

line.startYProperty().bind(
    source.layoutYProperty().add(
        Bindings.createDoubleBinding(height.divide(2)));
相关问题