将Custom FXML对象添加到另一个的问题

时间:2017-06-07 15:10:49

标签: java javafx javafx-2 fxml scenebuilder

简介

这只是一张“桌子”,可以通过右键点击桌子来“放置”卡片。

我有2个FXML对象:

  • UI

问题:

简而言之,需要将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对象的布局属性,如果它没有扩展堆栈窗格,我也无法将其作为子项添加到另一个对象

解决方案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

Card.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>

卡控制器

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强制转换异常

P.S。作为一个子问题,在这种情况下根节点有什么不同,当加载fxml对象时,它是否允许我例如如果我在定义中没有明确setlayoutX,那么extends StackPane属性是什么?

1 个答案:

答案 0 :(得分:2)

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

将其用作控制器类控制器
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>
相关问题