无法访问JavaFX GridPane setRow或setCol方法

时间:2018-11-17 17:11:08

标签: java javafx fxml scenebuilder

我正在尝试将imgShapeHolder移至不同的行,但无法使用GridPane setRow或setCol方法...

这是MainMenuController在ExitButtonEnter上的检查,这是我所能做的,但仍然出现错误。

package com.ljprogramming.app.controller;

import com.ljprogramming.app.Navigation;
import com.ljprogramming.app.util.enums.ButtonType;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.io.IOException;

public class MainMenuController implements Controller {

    private Navigation navigation;

    @FXML
    private BorderPane borderPane;

    @FXML
    private VBox vboxCenter;

    @FXML
    private HBox hboxCenterChild;

    @FXML
    private GridPane gridButtons;

    @FXML
    private ImageView imgShapeHolder;

    @FXML
    private ImageView imgPlayButton;

    @FXML
    private ImageView imgOptionsButton;

    @FXML
    private ImageView imgExitButton;

    @FXML
    private HBox vboxBottom;

    @FXML
    private VBox vboxRight;

    @FXML
    private VBox vboxTop;

    @FXML
    private HBox hboxTopChild;

    @FXML
    private ImageView imgTitle;

    @FXML
    private VBox vboxLeft;

    @FXML
    void onExitButtonClick(MouseEvent event) {
        System.exit(0);
    }

    @FXML
    void onExitButtonEnter(MouseEvent event) {
        int a = gridButtons.getChildren().indexOf(imgShapeHolder);
        gridButtons.getChildren().remove(gridButtons.getChildren().get(a));
        gridButtons.add(gridButtons.getChildren().get(a),0,1);
        //returns error saying Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=10.0, vgap=0.0, alignment=BOTTOM_RIGHT
    }

    public void setNavigation(Navigation navigation) {
        this.navigation = navigation;
    }
}

我正在使用Scenebuilder来设计布局,当我导出时,出现错误消息“在FXML Controller类中找不到可注入字段”,我不知道这是否与此有关。我检查了.xfml文件,所有fx:id都可以,并且控制器具有所有字段...

javafx error and the project structure image

.Scenebuilder生成的.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<BorderPane fx:id="borderPane" maxHeight="600.0" maxWidth="800.0" minHeight="600.0" minWidth="800.0" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ljprogramming.app.controller.MainMenuController">
   <center>
      <VBox fx:id="vboxCenter" prefHeight="337.0" prefWidth="533.0" BorderPane.alignment="CENTER">
         <children>
            <HBox fx:id="hboxCenterChild" alignment="CENTER" prefHeight="318.0" prefWidth="550.0" VBox.vgrow="NEVER">
               <children>
                  <GridPane fx:id="gridButtons" alignment="BOTTOM_RIGHT" hgap="10.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="353.0" prefWidth="432.0" HBox.hgrow="NEVER">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="235.0" minWidth="10.0" prefWidth="141.0" />
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="509.0" minWidth="10.0" prefWidth="432.0" />
                    </columnConstraints>
                    <rowConstraints>
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <ImageView fx:id="imgShapeHolder" fitHeight="63.0" fitWidth="63.0" pickOnBounds="true" preserveRatio="true" GridPane.halignment="RIGHT">
                           <image>
                              <Image url="@/img/mainmenu/shapes/PolygonShape.png" />
                           </image>
                        </ImageView>
                        <ImageView fx:id="imgPlayButton" fitHeight="49.0" fitWidth="163.0" onMouseClicked="#onPlayButtonClick" onMouseEntered="#onPlayButtonEnter" onMouseExited="#onPlayButtonExit" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.hgrow="NEVER" GridPane.vgrow="NEVER">
                           <image>
                              <Image url="@/img/mainmenu/buttons/PlayButton.png" />
                           </image>
                           <viewport>
                              <Rectangle2D minX="2.0" minY="2.0" />
                           </viewport>
                        </ImageView>
                        <ImageView fx:id="imgOptionsButton" fitHeight="51.0" fitWidth="258.0" onMouseClicked="#onOptionsButtonClick" onMouseEntered="#onOptionsButtonEnter" onMouseExited="#onOptionsButtonExit" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="1">
                           <image>
                              <Image url="@/img/mainmenu/buttons/OptionsButton.png" />
                           </image>
                        </ImageView>
                        <ImageView fx:id="imgExitButton" fitHeight="40.0" fitWidth="146.0" onMouseClicked="#onExitButtonClick" onMouseEntered="#onExitButtonEnter" onMouseExited="#onExitButtonExit" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="2">
                           <image>
                              <Image url="@/img/mainmenu/buttons/ExitButton.png" />
                           </image>
                        </ImageView>
                     </children>
                  </GridPane>
               </children>
            </HBox>
         </children>
      </VBox>
   </center>
   <bottom>
      <HBox fx:id="vboxBottom" alignment="CENTER" prefHeight="97.0" prefWidth="800.0" BorderPane.alignment="CENTER" />
   </bottom>
   <right>
      <VBox fx:id="vboxRight" prefHeight="334.0" prefWidth="159.0" BorderPane.alignment="CENTER" />
   </right>
   <top>
      <VBox fx:id="vboxTop" prefHeight="169.0" prefWidth="800.0" BorderPane.alignment="CENTER">
         <children>
            <HBox fx:id="hboxTopChild" alignment="CENTER" prefHeight="130.0" prefWidth="800.0">
               <children>
                  <ImageView fx:id="imgTitle" fitHeight="119.0" fitWidth="560.0" pickOnBounds="true" smooth="false">
                     <image>
                        <Image url="@/img/mainmenu/buttons/TitleButton.png" />
                     </image>
                  </ImageView>
               </children>
            </HBox>
         </children>
      </VBox>
   </top>
   <left>
      <VBox fx:id="vboxLeft" prefHeight="334.0" prefWidth="232.0" BorderPane.alignment="CENTER" />
   </left>
</BorderPane>

1 个答案:

答案 0 :(得分:0)

setRow中没有setColGridPane方法。


int a = gridButtons.getChildren().indexOf(imgShapeHolder);
gridButtons.getChildren().remove(gridButtons.getChildren().get(a));
gridButtons.add(gridButtons.getChildren().get(a),0,1);

这会导致重复的孩子异常或IndexOutOfBoundsException,因为删除一个孩子后,您从更新的孩子列表中检索了另一个孩子,或者使用了太大的索引(如果最后一个孩子被移除了。

您应使用GridPane.setRowIndexGridPane.setColumnIndex

GridPane.setRowIndex(imgShapeHolder, 1);
GridPane.setColumnIndex(imgShapeHolder, 0);