JavaFX GridPane动态添加元素

时间:2016-12-01 16:58:10

标签: java javafx

JavaFX新手在这里,我正在尝试动态创建一个GridPane,到目前为止我能够创建它指定以下内容:

GridPane gridPaneMain = new GridPane();

           gridPaneMain.add(button1, 0, 0, 1, 1);
           gridPaneMain.add(button2, 1, 0, 1, 1);
           gridPaneMain.add(button3, 2, 0, 1, 1);
           gridPaneMain.add(button4, 3, 0, 1, 1);
           gridPaneMain.add(button5, 4, 0, 1, 1);
           gridPaneMain.add(button6, 5, 0, 1, 1);
           gridPaneMain.add(button7, 6, 0, 1, 1);
           Scene scene = new Scene(gridPaneMain, 800, 800);

           ImageView imv1 = new ImageView();
           ImageView imv2 = new ImageView();
           ImageView imv3 = new ImageView();
           ImageView imv4 = new ImageView();
           ImageView imv5 = new ImageView();
           ImageView imv6 = new ImageView();
           ImageView imv7 = new ImageView();

           Image image1 = new Image(BoardGameMain.class.getResourceAsStream("viewcontrollers/blank.png"));
           imv1.setImage(image1);
           imv2.setImage(image1);
           imv3.setImage(image1);
           imv4.setImage(image1);
           imv5.setImage(image1);
           imv6.setImage(image1);
           imv7.setImage(image1);

           final HBox pictureRegion1 = new HBox();
           final HBox pictureRegion2 = new HBox();
           final HBox pictureRegion3 = new HBox();
           final HBox pictureRegion4 = new HBox();
           final HBox pictureRegion5 = new HBox();
           final HBox pictureRegion6 = new HBox();
           final HBox pictureRegion7 = new HBox();

           pictureRegion1.getChildren().add(imv1);
           pictureRegion2.getChildren().add(imv2);
           pictureRegion3.getChildren().add(imv3);
           pictureRegion4.getChildren().add(imv4);
           pictureRegion5.getChildren().add(imv5);
           pictureRegion6.getChildren().add(imv6);
           pictureRegion7.getChildren().add(imv7);

           gridPaneMain.add(pictureRegion1, 0, 1);
           gridPaneMain.add(pictureRegion2, 1, 1);
           gridPaneMain.add(pictureRegion3, 2, 1);
           gridPaneMain.add(pictureRegion4, 3, 1);
           gridPaneMain.add(pictureRegion5, 4, 1);
           gridPaneMain.add(pictureRegion6, 5, 1);
           gridPaneMain.add(pictureRegion7, 6, 1);

有关如何动态创建它而不指定每个元素的任何建议吗?

由于

0 个答案:

没有答案