简体   繁体   English

JavaFX ActionEvent 矩形

[英]JavaFX ActionEvent Rectangle

In short:简而言之:

I'm working on creating a card game with AI, and GUI.我正在使用 AI 和 GUI 创建纸牌游戏。 The User's hand is displayed on the game play interface, I have yet to complete the interface but I intend on adding a card face Image to rectangles on screen.用户的手显示在游戏界面上,我还没有完成界面,但我打算在屏幕上的矩形中添加一个卡面图像。 Rather than having 5 nearly identical methods, I found a similar post and I'm attempting to implement the same idea.我没有找到 5 个几乎相同的方法,而是找到了一个类似的帖子,我正在尝试实现相同的想法。 However, I ran into something unexpected.然而,我遇到了意想不到的事情。 Here's the related code from my controller class:这是我的控制器类中的相关代码:

/** This is where the players hand will be displayed. */
@FXML
private Rectangle card1, card2, card3, card4, card5;

public void playThisCard(final ActionEvent event) {
    if (event.getSource() == card1) {
        System.out.println("played card 1");
    } else if (event.getSource() == card2) {
        System.out.println("played card 2");
    } else if (event.getSource() == card3) {
        System.out.println("played card 3");
    } else if (event.getSource() == card4) {
        System.out.println("played card 4");
    } else if (event.getSource() == card5) {
        System.out.println("played card 5");
    } else {
        System.out.println("no card played");
    }
}

The UI was built in Scene Builder, hence the FXML tag on the Rectangles, the controller is correctly binded, and I've successfully gotten other methods to execute on user input to controls. UI 是在 Scene Builder 中构建的,因此矩形上的 FXML 标记,控制器已正确绑定,并且我已经成功地获得了其他方法来执行用户对控件的输入。

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
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:497)
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:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
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$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)

If you want to handle mouse clicks on Rectangle s you can use the onMouseClickedProperty .如果您想处理Rectangle的鼠标点击,您可以使用onMouseClickedProperty

So I guess in your FXML file you have something like this:所以我猜在你的 FXML 文件中你有这样的东西:

onMouseClicked="#playThisCard"

But if you take a look on the signature of the mentioned property:但是,如果您查看上述属性的签名:

public final ObjectProperty<EventHandler<? super MouseEvent>> onMouseClickedProperty

you will see it will not pass an ActionEvent but a MouseEvent , therefore你会看到它不会传递一个ActionEvent而是一个MouseEvent ,因此

public void playThisCard(final MouseEvent event)

is the correct signature to handle events got from a mouse click.是处理鼠标点击事件的正确签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM