首页 > 解决方案 > 无法访问 JavaFX GridPane setRow 或 setCol 方法

问题描述

我正在尝试将 imgShapeHolder 移动到不同的行,但我无法访问 GridPane setRow 或 setCol 方法...

这是 MainMenuController 检查 onExitButtonEnter,这就是我能做的,但我仍然得到错误。

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 控制器类中找不到可注入字段”,我不知道它是否与此有关。我检查了 .xfml 文件,所有 fx:id 都正常,并且控制器具有所有字段...

javafx错误和项目结构图

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>

标签: javajavafxfxmlscenebuilder

解决方案


中没有setRowsetCol方法GridPane


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

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

你应该使用GridPane.setRowIndexand GridPane.setColumnIndex

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

推荐阅读