gpt4 book ai didi

JavaFX ActionEvent 矩形

转载 作者:行者123 更新时间:2023-11-30 07:06:05 29 4
gpt4 key购买 nike

简而言之:

我正在致力于使用 AI 和 GUI 创建纸牌游戏。用户的手显示在游戏界面上,我尚未完成界面,但我打算将牌面图像添加到屏幕上的矩形中。我没有找到 5 种几乎相同的方法,而是找到了一篇类似的文章,并且我正在尝试实现相同的想法。然而,我遇到了意想不到的事情。这是我的 Controller 类的相关代码:

/** 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");
}
}

UI 是在 Scene Builder 中构建的,因此矩形上有 FXML 标记, Controller 已正确绑定(bind),并且我已成功获得其他方法来在用户输入控件时执行。

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)

最佳答案

如果您想处理矩形上的鼠标点击,您可以使用onMouseClickedProperty .

所以我猜你的 FXML 文件中有类似这样的内容:

onMouseClicked="#playThisCard"

但是如果你看一下上述属性的签名:

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

您将看到它不会传递 ActionEvent 而是传递 MouseEvent,因此

public void playThisCard(final MouseEvent event)

是处理鼠标单击事件的正确签名。

关于JavaFX ActionEvent 矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40107538/

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