我在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);
}
}
发布于 2018-06-06 01:09:20
getChildren()在父级中具有受保护的访问。看见
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"));
在同一个作用域中不能有两个同名的变量;编译器不会知道您引用的是哪个变量。
https://stackoverflow.com/questions/50705342
复制相似问题