gpt4 book ai didi

java - 将自定义 FXML 对象添加到另一个对象时出现问题

转载 作者:行者123 更新时间:2023-12-02 02:47:09 26 4
gpt4 key购买 nike

简介:

这只是一个“ table ”,可以通过右键单击 table 来“放置”卡片。

我有 2 个 FXML 对象:

  • 卡片
  • 用户界面

问题:

总之,需要在UI“Table”中添加一个Card,这就是我的问题所在。

添加Card方法只是将卡片添加到Pane对象(该对象不为空),并且我们知道Pane的添加扩展 Node

的对象

我尝试了两种解决方案,但都失败了。

解决方案1

使用 FXMLLoader 将 Card.fxml 文件加载到 Card 对象中:

卡片定义如下

public class Card implements Initializable

导致问题:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/Layouts/Card.fxml"));
Card card = loader.load();
card.setLayoutX(mouseEvent.getSceneX() - (card.getLayoutX() / 2)); <--------here setLayoutX
card.setLayoutY(mouseEvent.getSceneY() - (card.getLayoutY() / 2)); <--------here setLayoutY
card.isFaceDown.addListener((observable, oldValue, newValue) -> {
if (newValue) {
facedownCount.add(1);
} else {
facedownCount.subtract(1);
}
});
totalCount.add(1);
gamePane.getChildren().add(card); <--------here card does not extend Node

问题是,如果 Card 对象不扩展堆栈 Pane ,我将无法设置它的布局属性,而且我也无法将其作为子对象添加到另一个对象

解决方案 2:(更有可能是正确的)

将卡片定义更改为

public class Card extends StackPane implements Initializable

然后尝试添加它:

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/Layouts/Card.fxml"));
Card card = loader.load(); < -------- PROBLEM

这给了我这个异常错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 29 more
Caused by: java.lang.ClassCastException: javafx.scene.layout.StackPane cannot be cast to Game.Controller.Card
at Game.Controller.UI.HandleClick(UI.java:60)
... 39 more

卡片.fxml

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

<?import javafx.scene.layout.StackPane?>


<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.effect.DropShadow?>

<StackPane maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.112"
fx:controller="Game.Controller.Card"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fx:id="imgSuit" fitHeight="275.0" fitWidth="183.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="imgValue" fitHeight="275.0" fitWidth="183.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../suits/back.png" />
</image>
</ImageView>
</children>
<effect>
<DropShadow />
</effect>

</StackPane>

卡片 Controller

public class Card implements Initializable {

public static HashMap cardSuit, cardValue;

public BooleanProperty isFaceDown = new SimpleBooleanProperty(true);
private IntegerProperty value = new SimpleIntegerProperty(-1);
public StringProperty suit = new SimpleStringProperty("");
public ImageView imgValue = new ImageView();
public ImageView imgSuit = new ImageView();

public Card(){
super();
}

@Override
public void initialize(URL location, ResourceBundle resources) {
initImages();
setupBindings();
setDefaults();
}

private void setDefaults() {
isFaceDown.set(true);
}

private void setupBindings() {
isFaceDown.addListener((observable, oldValue, newValue) -> {
if (newValue){
imgValue.setImage(null);
imgSuit.setImage((Image) cardSuit.get("B"));
}
else {
imgValue.setImage((Image) cardValue.get(value.get()));
imgSuit.setImage((Image) cardSuit.get(suit.get()));
}
});
}

private void initImages() {
if (cardSuit == null) {
cardSuit = new HashMap<String, Image>();
cardSuit.put("B", new Image("/suits/back.png"));
cardSuit.put("C", new Image("/suits/clubs.png"));
cardSuit.put("D", new Image("/suits/diamonds.png"));
cardSuit.put("S", new Image("/suits/spades.png"));
cardSuit.put("H", new Image("/suits/hearts.png"));
}

if (cardValue == null) {
cardValue = new HashMap<Integer, Image>();
for (int i = 1; i <= 13; i++) {
cardValue.put(i, new Image("/values/" + String.valueOf(i) + ".png"));
}
}
}
}

UI fxml

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>

<?import javafx.scene.layout.StackPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMousePressed="#HandleClick" prefHeight="750.0" prefWidth="702.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Game.Controller.UI">
<children>
<MenuBar prefHeight="29.0" prefWidth="700.0">
<menus>
<Menu mnemonicParsing="false" text="Menu">
<items>
<MenuItem fx:id="mmReset" mnemonicParsing="false" text="Reset" />
<MenuItem fx:id="mmClose" mnemonicParsing="false" onAction="#CloseApp" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem fx:id="mmAbout" mnemonicParsing="false" text="About" />
</items>
</Menu>
<Menu fx:id="mInstruct" mnemonicParsing="false" text="Instructions" />
</menus>
</MenuBar>
<StackPane fx:id="gamePane" layoutX="1.0" layoutY="29.0" prefHeight="700.0" prefWidth="700.0" style="-fx-background-color: green;" />
<HBox layoutX="7.0" layoutY="731.0" prefHeight="15.0" prefWidth="693.0">
<children>
<Label fx:id="lblHearts" text="Hearts = " />
<Label fx:id="lblDiamonds" text="Diamonds = " />
<Label fx:id="lblSpades" text="Spades = " />
<Label fx:id="lblClubs" text="Clubs = " />
<Label fx:id="lblFaceUp" text="Facing Up =" />
</children>
</HBox>
</children>
</AnchorPane>

我做错了什么,我认为解决方案#2更“正确”,但为什么加载器不允许加载到卡中,并出现 StackPane 强制转换异常

附:作为一个子问题,在这种情况下,根节点在加载 fxml 对象时有什么区别,它是否允许我例如setlayoutX 属性,如果我没有在定义中显式extends StackPane

最佳答案

您可以创建一个扩展 StackPane 的自定义组件并通过使用 <fx:root> 用作 Controller 类 Controller 元素

public class Card extends StackPane implements Initializable {
public Card() {
FXMLLoader loader = new FXMLLoader(getClass().getResouce("card.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (Exception e) {
throw new IllegalStateException("Could not load fxml", e);
}
}

...

}

card.fxml

<fx:root type="javafx.scene.layout.StackPane"
maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fx:id="imgSuit" fitHeight="275.0" fitWidth="183.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="imgValue" fitHeight="275.0" fitWidth="183.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../suits/back.png" />
</image>
</ImageView>
</children>
<effect>
<DropShadow />
</effect>
</fx:root>

关于java - 将自定义 FXML 对象添加到另一个对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44416509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com