首页 > 解决方案 > JavaFX - FXML 文件中的自定义对象

问题描述

我的代码中有以下扩展的 JavaFX 对象:

package shipsinspace.view.board;

import javafx.scene.shape.Rectangle;
import shipsinspace.common.Coordinates;

public class Tile extends Rectangle {
    private Coordinates coordinates;

    public Tile(double width, double height, Coordinates coordinates) {
        super(width, height);
        this.coordinates = coordinates;
    }

    public Coordinates getCoordinates() {
        return coordinates;
    }
}

它使用我编写的这个客户 Java 类来跟踪 Tile 位置:

package shipsinspace.common;

import java.util.Objects;

public class Coordinates {
    private int xCoordinate;
    private int yCoordinate;

    public Coordinates(int xCoordinate, int yCoordinate) {
        this(xCoordinate, yCoordinate, 10, false);
    }

    public Coordinates(int xCoordinate, int yCoordinate, int max) {
        this(xCoordinate, yCoordinate, max, false);
    }

    public Coordinates(int xCoordinate, int yCoordinate, int max, boolean allowedZero) {
        if (allowedZero) {
            if ((xCoordinate >= 0 && yCoordinate >= 0) && (xCoordinate <= max && yCoordinate <= max)) {
                this.xCoordinate = xCoordinate;
                this.yCoordinate = yCoordinate;
            } else {
                throw new IllegalArgumentException(String.format("Either X or Y has set to value <= 0, or > %d", max));
            }
        } else {
            if ((xCoordinate > 0 && yCoordinate > 0) && (xCoordinate <= max && yCoordinate <= max)) {
                this.xCoordinate = xCoordinate;
                this.yCoordinate = yCoordinate;
            } else {
                throw new IllegalArgumentException(String.format("Either X or Y has set to value <= 0, or > %d", max));
            }
        }

    }

    public int getX() {
        return xCoordinate;
    }

    public int getY() {
        return yCoordinate;
    }

    public Coordinates returnNeighbour(int axis, int direction) {
        if (axis == 0) {
            try {
                return new Coordinates(this.getX() + direction, this.getY());
            } catch (IllegalArgumentException e) {
                return new Coordinates(this.getX(), this.getY());
            }

        } else {
            try {
                return new Coordinates(this.getX(), this.getY() + direction);
            } catch (IllegalArgumentException e) {
                return new Coordinates(this.getX(), this.getY());
            }

        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Coordinates that = (Coordinates) o;
        return xCoordinate == that.xCoordinate &&
                yCoordinate == that.yCoordinate;
    }

    @Override
    public int hashCode() {
        return Objects.hash(xCoordinate, yCoordinate);
    }

    @Override
    public String toString() {
        return String.format("Coordinates (%d, %d)", xCoordinate, yCoordinate);
    }
}

现在,我想构建一个场景(通过 JavaFX Scene Builder),它使用 GridPane,每个单元格中都有一个 TILE 对象。我决定首先在 Scene Builder 中构建一个场景,使用 JavaFX Rectangle 对象而不是 Tiles,然后手动编辑 .fxml 文件并将 Rectangle 更改为 Tile 对象。问题是,Intellij 现在告诉我 FXML 文件中的 Tile 对象无法实例化:

...
   <center>
      <GridPane BorderPane.alignment="CENTER">
        <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.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 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 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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>

            <Tile arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="52.0" stroke="BLACK" strokeType="INSIDE" width="53.0" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
               <GridPane.margin>
                  <Insets bottom="1.0" left="1.0" right="1.0" top="1.0" />
               </GridPane.margin>
            </Tile>
...

我可以说,这是因为我的 Tile 对象需要将 Coordinates 对象传递到其构造函数中,而不是高度和宽度,但我不知道如何将它们粘贴到 FXML 代码中。任何帮助将不胜感激。

标签: javajavafx

解决方案


要允许在FXMLLoader没有无参数构造函数的情况下实例化类,您需要注释构造函数参数。(原因是在 Java 中参数名称不能保证在运行时保留,因此需要一种机制在运行时根据名称反射性地将无序值与参数匹配。)请参阅javaFX 中 @NamedArg 注释的目的是什么8?了解更多信息。

所以你TileCoordinates类现在看起来像:

import javafx.beans.NamedArg;
import javafx.scene.shape.Rectangle;

public class Tile extends Rectangle {
    private Coordinates coordinates;

    public Tile(
            @NamedArg("width") double width,
            @NamedArg("height") double height,
            @NamedArg("coordinates") Coordinates coordinates) {
        super(width, height);
        this.coordinates = coordinates;
    }

    public Coordinates getCoordinates() {
        return coordinates;
    }
}
import java.util.Objects;

import javafx.beans.NamedArg;

public class Coordinates {
    private int xCoordinate;
    private int yCoordinate;

    public Coordinates(
            @NamedArg("xCoordinate") int xCoordinate,
            @NamedArg("yCoordinate") int yCoordinate) {
        this(xCoordinate, yCoordinate, 10, false);
    }

    public Coordinates(
            @NamedArg("xCoordinate") int xCoordinate, 
            @NamedArg("yCoordinate") int yCoordinate,
            @NamedArg("max") int max) {
        this(xCoordinate, yCoordinate, max, false);
    }

    public Coordinates(
            @NamedArg("xCoordinate") int xCoordinate,
            @NamedArg("yCoordinate") int yCoordinate,
            @NamedArg("max") int max,
            @NamedArg("allowedZero") boolean allowedZero) {
        if (allowedZero) {
            if ((xCoordinate >= 0 && yCoordinate >= 0) && (xCoordinate <= max && yCoordinate <= max)) {
                this.xCoordinate = xCoordinate;
                this.yCoordinate = yCoordinate;
            } else {
                throw new IllegalArgumentException(String.format("Either X or Y has set to value <= 0, or > %d", max));
            }
        } else {
            if ((xCoordinate > 0 && yCoordinate > 0) && (xCoordinate <= max && yCoordinate <= max)) {
                this.xCoordinate = xCoordinate;
                this.yCoordinate = yCoordinate;
            } else {
                throw new IllegalArgumentException(String.format("Either X or Y has set to value <= 0, or > %d", max));
            }
        }

    }

    // remaining code unaltered...
}

要在 FXML 中使用它,您可以执行以下操作:

<Tile width="100.0" height="100.0">
    <coordinates>
        <Coordinates xCoordinate="1" yCoordinate="1"/>
    </coordinates>
</Tile>

或者

<fx:define>
    <Coordinates fx:id="tileCoordinates" xCoordinate="1" yCoordinate="1" />
</fx:define>
<Tile width="100.0" height="100.0" coordinates="$tileCoordinates" />

这些之间的选择大多只是风格的选择;但是请注意,后一个选项使您有机会让多个Tiles 共享同一个Coordinates实例(可能不适用于此特定用例,但通常它很有用)。


推荐阅读