nai*_*jed 8 java javafx javafx-2
如何循环播放场景的控件?我尝试使用getChildrenUnmodifiable()但它只返回第一级子级.
public void rec(Node node){
f(node);
if (node instanceof Parent) {
Iterator<Node> i = ((Parent) node).getChildrenUnmodifiable().iterator();
while (i.hasNext()){
this.rec(i.next());
}
}
}
Run Code Online (Sandbox Code Playgroud)
您需要递归扫描.例如:
private void scanInputControls(Pane parent) {
for (Node component : parent.getChildren()) {
if (component instanceof Pane) {
//if the component is a container, scan its children
scanInputControls((Pane) component);
} else if (component instanceof IInputControl) {
//if the component is an instance of IInputControl, add to list
lstInputControl.add((IInputControl) component);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的amru答案的修改版本,此方法为您提供特定类型的组件:
private <T> List<T> getNodesOfType(Pane parent, Class<T> type) {
List<T> elements = new ArrayList<>();
for (Node node : parent.getChildren()) {
if (node instanceof Pane) {
elements.addAll(getNodesOfType((Pane) node, type));
} else if (type.isAssignableFrom(node.getClass())) {
//noinspection unchecked
elements.add((T) node);
}
}
return Collections.unmodifiableList(elements);
}
Run Code Online (Sandbox Code Playgroud)
要获得所有组件:
List<Node> nodes = getNodesOfType(pane, Node.class);
Run Code Online (Sandbox Code Playgroud)
只获得按钮:
List<Button> buttons= getNodesOfType(pane, Button.class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4271 次 |
| 最近记录: |