热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Vbox和Hbox在什么情况下为其子元素分配坐标?-在哪些场景下Vbox和Hbox会为其子组件分配坐标值?

在Vbox和Hbox布局中,当用户点击容器添加一个矩形时,系统会自动为该矩形分配坐标并打印其位置信息。此外,在按键事件触发时,系统仅打印当前矩形的坐标值。这两种布局在特定的交互场景下,能够动态地管理和更新子组件的位置。

I put rectangle on a vbox on click and I print coordinate of the rectangle. On key pressed I only print the coordinate. Look at this sample code example :

我点击vbox上的矩形,我打印矩形的坐标。按下键时我只打印坐标。看看这个示例代码示例:

@FXML
private VBox vbox;

@FXML
private AnchorPane anchorpane;

private List rectangles = new ArrayList<>();

@Override
public void initialize(URL location, ResourceBundle resources) {
    System.out.println("init");

}

@FXML
private void onMouseClick(MouseEvent e) {
    System.out.println("click");
    vbox.getChildren().clear();

    for(int i = 0; i <3 ; i++){
        Rectangle r = new Rectangle(100.0, 10.0, Color.BLACK);
        r.addEventHandler(KeyEvent.ANY, this::onKeyTyped);
        rectangles.add(r);
        vbox.getChildren().add(r);

        System.out.println(" r Yposition :" + r.getLayoutY() + " or " + r.getBoundsInParent().getMinY() + " or " + r.getBoundsInLocal().getMinY());
        System.out.println("Vbox height : " + vbox.getBoundsInLocal().getHeight());

    }

}

@FXML
private void onKeyTyped(KeyEvent e) {
    System.out.println("Key pressed");
    for (Rectangle r : rectangles){         
        System.out.println(" r Yposition :" + r.getLayoutY() + " or " + r.getBoundsInParent().getMinY() + " or " + r.getBoundsInLocal().getMinY());
        }
    System.out.println("Vbox height : " + vbox.getBoundsInLocal().getHeight());
}

This give me the following output :

这给了我以下输出:

click
r Yposition :0.0 or 0.0 or 0.0
Vbox height : 10.0
r Yposition :0.0 or 0.0 or 0.0
Vbox height : 10.0
 r Yposition :0.0 or 0.0 or 0.0
Vbox height : 10.0

Key pressed
 r Yposition :0.0 or 0.0 or 0.0
 r Yposition :10.0 or 10.0 or 0.0
 r Yposition :20.0 or 20.0 or 0.0
Vbox height : 30.0

So I suppose JavaFX assign coordinate of the shape when it prints it on the screen. But how can I have the coordinate of the shape in the Vbox when I refresh it ?

所以我想JavaFX在屏幕上打印时会分配形状的坐标。但是当我刷新它时,如何在Vbox中获得形状的坐标?

1 个解决方案

#1


After you add the rectangles to the VBox, if you call inmediately to getBoundsInParent() you get no results.

将矩形添加到VBox后,如果您立即调用getBoundsInParent(),则不会得到任何结果。

The reason for this can be found here:

原因可以在这里找到:

Layout and CSS are also tied to pulse events. Numerous changes in the scene graph could lead to multiple layout or CSS updates, which could seriously degrade performance. The system automatically performs a CSS and layout pass once per pulse to avoid performance degradation. Application developers can also manually trigger layout passes as needed to take measurements prior to a pulse.

布局和CSS也与脉冲事件有关。场景图中的大量更改可能导致多个布局或CSS更新,这可能会严重降低性能。系统每个脉冲自动执行一次CSS和布局传递,以避免性能下降。应用程序开发人员还可以根据需要手动触发布局过程,以便在脉冲之前进行测量。

So if you really need the bounds of those rectangles in the same pass, you can call:

因此,如果你真的需要在同一个传递中的那些矩形的边界,你可以调用:

vbox.layout();

right after adding each rectangle to the box to get its final position.

在将每个矩形添加到框中以获得其最终位置之后。

According to Javadoc, layout():

根据Javadoc,layout():

Executes a top-down layout pass on the scene graph under this parent. Calling this method while the Parent is doing layout is a no-op

在此父级下的场景图上执行自上而下的布局传递。在Parent进行布局时调用此方法是一种无操作

And now this will work as you would expected:

现在,这将按照您的预期运作:

for(int i = 0; i <3 ; i++){
    Rectangle r = new Rectangle(100.0, 10.0, Color.BLACK);
    rectangles.add(r);
    vbox.getChildren().add(r);
    vbox.layout();

    System.out.println(" r Yposition :" + r.getLayoutY() + " or " + r.getBoundsInParent().getMinY() + " or " + r.getBoundsInLocal().getMinY());
    System.out.println("Vbox height : " + vbox.getBoundsInLocal().getHeight());
}

Alternatively, you could override the layoutChildren() method of vbox, as it is invoked during the layout pass to layout the children in this parent:

或者,您可以覆盖vbox的layoutChildren()方法,因为在布局传递期间调用它来布局此父级中的子级:

private VBox vbox=new VBox(){

    @Override
    protected void layoutChildren() {
        super.layoutChildren(); 
        for (Rectangle r : rectangles){         
            System.out.println(" r Yposition :" + r.getLayoutY() + " or " + r.getBoundsInParent().getMinY() + " or " + r.getBoundsInLocal().getMinY());
        }
    }

};

It will give you the same result. Note that this won't work with vbox added to the FXML file, since you need to create a new instance of it.

它会给你相同的结果。请注意,这不适用于添加到FXML文件的vbox,因为您需要创建它的新实例。


推荐阅读
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 将Web服务部署到Tomcat
    本文介绍了如何在JDeveloper 12c中创建一个Java项目,并将其打包为Web服务,然后部署到Tomcat服务器。内容涵盖从项目创建、编写Web服务代码、配置相关XML文件到最终的本地部署和验证。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
author-avatar
广东暖暖_小乖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有