【问题标题】:JavaFx GetChildern() function not avalibleJavaFx GetChildren() 函数不可用
【发布时间】:2018-06-05 16:57:03
【问题描述】:

我在 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);
    }
}

【问题讨论】:

  • 该方法不是public in Parent(在PaneGroup 中制作public)。您也不能声明 2 个具有相同名称的变量。

标签: java user-interface javafx


【解决方案1】:

getChildren() 在 Parent 中具有受保护的访问权限。见

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"));

在同一个作用域内不能有两个同名的变量;编译器不会知道你引用的是哪个变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多