使用getChildren()时遇到问题

时间:2016-04-02 02:11:49

标签: java javafx gridpane

我正在为一所学校项目工作,而我在将父窗格添加到父窗格时遇到了问题。所有代码都会编译,除非我到了pane.getChildren()。add(Matrix); 。当我在main中有所有代码时,我能够获得编译代码,但我真的想要主要调用一个类并在那里创建窗格然后将其添加到父窗格。我现在不要担心它看起来很漂亮,只是想找到一种方法让它发挥作用。如果有人能帮助我朝着正确的方向前进,我会非常感激。

编译器给了我

Button1.java:34:错误:预期的标识符

pane.getChildren()添加(矩阵);

Button1.java:34:错误:';'预期

pane.getChildren()添加(矩阵);

 public class Button1 extends Application {
public void start(Stage primaryStage) {


Scene scene = new Scene(pane, 700, 500);
primaryStage.setTitle("3 pains 1 window "); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
 primaryStage.show(); // Display the stage
}
 public static void main(String[] args) {
Application.launch(args);
}
GridPane pane = new GridPane();
MatrixPane Matrix = new MatrixPane();
pane.getChildren().add(Matrix);

}

class MatrixPane extends Pane {

double HEIGHT = 500;
double WIDTH = 200;
private GridPane pane = new GridPane();


public MatrixPane() {
}

public void fillpane() {

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            TextField text = new TextField(Integer.toString((int)(Math.random() * 2)));
            text.setMinWidth(WIDTH / 8.0);
            text.setMaxWidth(WIDTH / 10.0);
            text.setMinHeight(HEIGHT / 8.0);
            text.setMaxHeight(HEIGHT / 10.0);
                pane.add(text, j, i);
        }
    }

}


}

2 个答案:

答案 0 :(得分:2)

该行必须在方法内部,我建议它应该在开始

之内
public void start(Stage primaryStage) {
   pane.getChildren().add(Matrix);
   ...

答案 1 :(得分:-2)

您错过了在方法()中包含以下部分。

相关问题