JavaFx GetChildern()函数不可用

时间:2018-06-05 16:57:04

标签: java user-interface javafx

我在Javafx中找不到getChildren()元素来在容器中添加按钮或文本。 我试图搜索一个类,但我找不到任何解决这个问题的方法 任何解决方案?

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.awt.*;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Group root = new Group();
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        Text txt = new Text("Mohanned");
        root.getChildren().add(txt);
        txt.setY(50);
        root.getChildrenUnmodifiable().add(txt);

        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

1 个答案:

答案 0 :(得分:2)

getChildren()在Parent中保护了access。见

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Parent.html#getChildren--

了解更多信息。

还有一个问题,

Group root = new Group();
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

在同一范围内,您不能拥有两个具有相同名称的变量;编译器不知道你正在考虑哪个变量。