【问题标题】:Having trouble using getChildren()使用 getChildren() 时遇到问题
【发布时间】:2016-07-21 23:22:25
【问题描述】:

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

编译器给我的

Button1.java:34:错误:需要标识符

pane.getChildren().add(矩阵);

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

pane.getChildren().add(矩阵);

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

}


}

【问题讨论】:

    标签: java javafx gridpane


    【解决方案1】:

    该行必须在方法内,我建议它应该在 start 内

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 你不能把它放在mainApplication.launch 阻塞直到应用程序关闭,main 不在 FX 应用程序线程上执行。
      • 此外,将这些行添加到 main 不会使除 main 之外的任何方法都可以访问这些局部变量。字段中的值不会受到这些方法调用的影响。
      猜你喜欢
      • 1970-01-01
      • 2012-02-21
      • 2021-11-08
      • 2014-01-08
      • 2011-03-09
      • 2013-01-30
      • 2016-11-13
      • 2013-05-10
      相关资源
      最近更新 更多