在JavaFX中动态添加组件 [英] Add components dynamically in JavaFX

查看:3612
本文介绍了在JavaFX中动态添加组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:问题不正确,因为对主题的了解不足。抱歉。

我正在尝试创建一个小应用程序,显示包含节点的图表和他们之间的连接。节点和连接都很复杂,我的意思是它们可以有其他组件,比如标签。

I'm trying to create a small application that shows a graph that contains nodes and connections between them. Both nodes and connections are complex, I mean they can have another components in it, like labels.

我有一个扮演画布角色的大窗格。我要添加和删除元素。问题是我想使用按钮或上下文菜单动态添加删除图形元素。排序绘制图形 :)我不知道如何实现它。
特别是我迫切需要动态添加/删除机制的帮助。我非常感谢你的帮助!

I have a big Pane which plays a canvas role. I'm going to add and remove elements from it. The problem is that I want to add or remove graph elements dynamically, using buttons or context menu. Sort of Paint for Graphs :) And I have no idea how to implement it. Especially I desperately need help in dynamical adding/removing mechanism. I would be very grateful for your help!

推荐答案

只需获取要添加内容的窗格的子列表,在适当的操作发生时添加内容。

Just get the child list of the pane you want to add stuff to, and add the stuff when the appropriate action occurs.

FlowPane pane = new FlowPane();
Button addNode = new Button("Add");
addNode.setOnAction(e -> pane.getChildren().add(new Circle(10));

注意:


  • 如果你想使用窗格而不是 FlowPane ,然后Pane没有内部布局,所以你还需要设置 layoutX 和<添加时,a href =https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#layoutYProperty-- =nofollow> layoutY 属性到窗格。

  • If you want to use a Pane rather than a FlowPane, then Pane has no internal layout, so you need to also set the layoutX and layoutY properties appropriately when you add to the Pane.

如果要更改窗格中节点的渲染顺序(例如,哪些节点在底部渲染,哪些节点在顶部渲染),然后你通过在ch中的适当位置添加新节点来做到这一点ild list;例如, pane.getChildren()。add(0,new Circle(10)),将添加一个在窗格的所有其他子项下面呈现的圆,而不是顶部。

If you want to change the rendering order of nodes in the Pane (e.g. which nodes are rendered on the bottom and which on top), then you do that by adding new nodes at an appropriate position in the child list; for example, pane.getChildren().add(0, new Circle(10)), will add a circle that is rendered underneath all other children of the pane rather than on top.

这篇关于在JavaFX中动态添加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆