如何在 JavaFX 中访问对象的子级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26811445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:20:06  来源:igfitidea点击:

How to access a child of an object in JavaFX

javajavafx

提问by Winged

Let's say I have created an object which can have children and have getChildren()method, f.e. Group(). Then I created an another object which can 'store' children, too, f.e. VBox(). And then again I create yet an another object, f.e. Slider().

假设我创建了一个可以有子项和getChildren()方法的对象fe Group()。然后我创建了另一个也可以“存储”孩子的对象 fe VBox()。然后我再次创建了另一个对象 fe Slider()

So now I add the Slider object to the list of VBox children by calling myVBox.getChildren().add(mySlider);, and then I add the VBox object to the list of Group object. Supose that everything is performed inside a function that returns myGroupobject.

所以现在我通过调用将 Slider 对象添加到 VBox 子项列表中myVBox.getChildren().add(mySlider);,然后将 VBox 对象添加到 Group 对象列表中。假设一切都在返回myGroup对象的函数中执行。

Now I'm outside of the function, I have no direct way to access Slider properties, I need to access Group children, get the VBox, then get the Slider from VBox children.

现在我在函数之外,我没有直接访问 Slider 属性的方法,我需要访问 Group 子级,获取 VBox,然后从 VBox 子级获取 Slider。

So as far as I understand, I should call myGroup.getChildren().get(0);to get the first child added (which in this case should be the VBox object). Now I need to go deeper, so I should call myGroup.getChildren().get(0).getChildren().get(0);, right?

所以据我所知,我应该打电话myGroup.getChildren().get(0);来添加第一个孩子(在这种情况下应该是 VBox 对象)。现在我需要更深入,所以我应该打电话给myGroup.getChildren().get(0).getChildren().get(0);,对吗?

Unfortunately the object returned by myGroup.getChildren().get(0);does not have a getChildren()method and it's type of Node class, while myGroup.getChildren().get(0).getClass();returns info that that child is type of VBox.

不幸的是,返回的对象myGroup.getChildren().get(0);没有getChildren()方法,它是 Node 类的类型,而myGroup.getChildren().get(0).getClass();返回的信息是该子类是 VBox 类型。

I'm quite a freshmen in Java, so please, so please, point out my misconceptions.

我是 Java 的大一新生,所以请指出我的误解。

采纳答案by José Pereda

Assuming you have a Sliderinside a VBoxwith other nodes, and this box is inside a group, you can access the inner slider with getChildren()by casting the resulting node to its type. And before that, make sure you can do this casting by cheking if the node is instance of the specific class with instanceof.

假设你有一个带有其他节点的Sliderinside VBox,并且这个框在一个组内,你可以getChildren()通过将结果节点转换为它的类型来访问内部滑块。在此之前,请确保您可以通过检查节点是否是特定类的实例来执行此转换instanceof

This simple example will help you.

这个简单的例子会对你有所帮助。

private final Group group = new Group();
private final VBox vbox = new VBox();
private final Button button = new Button("Click");
private final Label label = new Label("Slider Value: ");

@Override
public void start(Stage primaryStage) {
    vbox.getChildren().addAll(button, label, new Slider(0,10,4));
    vbox.setSpacing(20);
    group.getChildren().add(vbox);

    button.setOnAction(e->{
        Node nodeOut = group.getChildren().get(0);
        if(nodeOut instanceof VBox){
            for(Node nodeIn:((VBox)nodeOut).getChildren()){
                if(nodeIn instanceof Slider){
                    label.setText("Slider value: "+((Slider)nodeIn).getValue());
                }
            }

        }      
    });
    Scene scene = new Scene(group, 300, 250);
    primaryStage.setScene(scene);
    primaryStage.show();
}