无法访问 JavaFX GridPane setRow 或 setCol 方法
Can't acess JavaFX GridPane setRow or setCol methods
我正在尝试将 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设计布局,当我导出时出现错误提示"No injectable field found in FXML Controller class",我不知道是否与此有关。我检查了 .xfml 文件,所有 fx:id 都正常,并且控制器具有所有字段...
javafx error and the project structure image
.fxml 由 Scenebuilder 生成
<?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>
GridPane
中没有setRow
和setCol
方法。
int a = gridButtons.getChildren().indexOf(imgShapeHolder);
gridButtons.getChildren().remove(gridButtons.getChildren().get(a));
gridButtons.add(gridButtons.getChildren().get(a),0,1);
这会导致 重复 children 异常或 IndexOutOfBoundsException
因为在删除 child 之后,您会检索另一个 child 来自更新的 child 列表或使用过大的索引,如果删除了最后一个 child。
你应该使用 GridPane.setRowIndex
and GridPane.setColumnIndex
:
GridPane.setRowIndex(imgShapeHolder, 1);
GridPane.setColumnIndex(imgShapeHolder, 0);
我正在尝试将 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设计布局,当我导出时出现错误提示"No injectable field found in FXML Controller class",我不知道是否与此有关。我检查了 .xfml 文件,所有 fx:id 都正常,并且控制器具有所有字段...
javafx error and the project structure image
.fxml 由 Scenebuilder 生成
<?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>
GridPane
中没有setRow
和setCol
方法。
int a = gridButtons.getChildren().indexOf(imgShapeHolder);
gridButtons.getChildren().remove(gridButtons.getChildren().get(a));
gridButtons.add(gridButtons.getChildren().get(a),0,1);
这会导致 重复 children 异常或 IndexOutOfBoundsException
因为在删除 child 之后,您会检索另一个 child 来自更新的 child 列表或使用过大的索引,如果删除了最后一个 child。
你应该使用 GridPane.setRowIndex
and GridPane.setColumnIndex
:
GridPane.setRowIndex(imgShapeHolder, 1);
GridPane.setColumnIndex(imgShapeHolder, 0);