【问题标题】:What happens when you call getChildren on it's own?当您自己调用 getChildren 时会发生什么?
【发布时间】:2018-02-15 07:06:41
【问题描述】:

我对 Java 比较陌生,尤其是 Javafx 和 GUI。我一直在研究这段代码,但我在理解 THE VERY LAST LINE 的作用时遇到了一些麻烦。我知道倒数第二行是将所有组件添加到容器“p”中,但是当您调用 getChildren() 之前没有容器时会发生什么。无论如何,任何帮助表示赞赏。

import javafx.scene.control.Button;
import javafx.scene.layout.Pane;

public class DVDButtonPane extends Pane {

    public DVDButtonPane(){
        Pane p = new Pane();

        Button add = new Button("Add");
        add.setPrefSize(95, 30);
        add.relocate(0, 0);
        add.setStyle("-fx-font: 12 arial; -fx-base: rgb(0,100,0); -fx-text-fill: rgb(255,255,255);");

        Button delete = new Button("Delete");
        delete.setPrefSize(90, 30);
        delete.relocate(100, 0);
        delete.setStyle("-fx-font: 12 arial; -fx-base: rgb(100,0,0); -fx-text-fill: rgb(255,255,255);");

        Button stats = new Button("Stats");
        stats.setPrefSize(90, 30);
        stats.relocate(210, 0);

        p.getChildren().addAll(add, delete, stats);
        getChildren().add(p);
    }
}

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    getChildren 与调用this.getChildren 相同。该行将p 添加到DVDButtonPane 的子集合中。

    【讨论】:

      【解决方案2】:

      它将“p”添加到 DVDButtonPane。但如果 DVDButtonPane 本身是从 Pane 扩展而来的。您可以直接将按钮添加到 DVDButtonPane

      【讨论】:

        猜你喜欢
        • 2016-11-29
        • 1970-01-01
        • 1970-01-01
        • 2017-02-20
        • 1970-01-01
        • 2016-09-15
        • 2017-07-12
        • 2016-11-11
        • 2010-12-07
        相关资源
        最近更新 更多