如何在我的GridPane上使用onMouseClicked修复“非法参数异常?”

问题描述 投票:1回答:1
对于Java等新手的强制性道歉。在此之后,我试图开发一个简单的游戏。目前,我要做的就是能够在板上移动计数器,但是每次执行此操作都会收到错误消息。这似乎与我在游戏板上使用的GridPane的onMouseClicked属性有关,因为即使handleMovement方法为空,只要单击鼠标,我仍然会收到所有这些错误:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch [java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [java] at java.lang.reflect.Method.invoke(Method.java:498) [java] at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) [java] at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) [java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [java] at java.lang.reflect.Method.invoke(Method.java:498) [java] at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) [java] at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) [java] at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) [java] at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) [java] at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) [java] at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) [java] at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) [java] at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) [java] at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [java] at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [java] at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [java] at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) [java] at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) [java] at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) [java] at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) [java] at javafx.event.Event.fireEvent(Event.java:198) [java] at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470) [java] at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398) [java] at javafx.scene.Scene$MouseHandler.process(Scene.java:3766) [java] at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) [java] at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) [java] at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) [java] at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394) [java] at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) [java] at java.security.AccessController.doPrivileged(Native Method) [java] at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432) [java] at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410) [java] at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431) [java] at com.sun.glass.ui.View.handleMouseEvent(View.java:555) [java] at com.sun.glass.ui.View.notifyMouse(View.java:937) [java] at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) [java] at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186) [java] at java.lang.Thread.run(Thread.java:748)

我的控制器类:

package Particle; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.layout.*; import javafx.scene.shape.Circle; import Elements.Player; import javafx.scene.Node; public class GameInterfaceController implements Initializable { //prepare new game grid @FXML GridPane gameBoard = new GridPane(); //prepare player marker @FXML Circle playerCirc = new Circle(); /* * Initialise controller class * Requires to call teleport method, otherwise player will always begin at (0,0) */ @Override public void initialize(URL url, ResourceBundle rb) { teleport(); } /* * Handles action when user clicks "Teleport" button */ @FXML private void handleTeleport(ActionEvent event) throws Exception { teleport(); } @FXML private void handleMovement(ActionEvent event) throws Exception { Node source = (Node)event.getSource(); System.out.println(source.toString()); Integer colIndex = GridPane.getColumnIndex(source); Integer rowIndex = GridPane.getRowIndex(source); System.out.printf("Mouse entered cell [%d, %d]%n", colIndex.intValue(), rowIndex.intValue()); } /* * move player marker to a random location on the board */ private void teleport() { GridPane.setColumnIndex(playerCirc, (int)(12.0 * Math.random())); GridPane.setRowIndex(playerCirc, (int)(20.0 * Math.random())); }

}

我的FXML:

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.geometry.Insets?> <?import javafx.scene.control.Button?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.ColumnConstraints?> <?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.shape.Circle?> <?import javafx.scene.shape.MeshView?> <AnchorPane id="AnchorPane" fx:id="gameWindow" prefHeight="709.0" prefWidth="380.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Particle.GameInterfaceController"> <children> <GridPane fx:id="gameBoard" gridLinesVisible="true" layoutX="14.0" layoutY="14.0" maxHeight="400.00" maxWidth="240.00" minHeight="600.0" minWidth="360.0" onMouseClicked="#handleMovement" prefHeight="400.0" prefWidth="240.0" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="10.0"> <columnConstraints> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="NEVER" minWidth="10.0" prefWidth="100.0" /> </columnConstraints> <rowConstraints> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="NEVER" /> </rowConstraints> <children> <Circle fx:id="playerCirc" fill="#ff4326" radius="10.0" stroke="BLACK" strokeType="INSIDE"> <GridPane.margin> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> </GridPane.margin> </Circle> </children> </GridPane> <Button fx:id="teleportButton" layoutX="14.0" layoutY="625.0" mnemonicParsing="false" onAction="#handleTeleport" text="Teleport" /> <Button fx:id="pulseButton" layoutX="90.0" layoutY="625.0" mnemonicParsing="false" onAction="#handlePulse" prefHeight="25.0" prefWidth="61.0" text="EMP" /> <MeshView layoutX="233.0" layoutY="657.0" onMouseClicked="#handleMovement" /> </children> </AnchorPane>

我的主班:

package Particle; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) throws Exception { //prepare game interface Parent root = FXMLLoader.load(getClass().getResource("GameInterface.fxml")); Scene gameWindow = new Scene(root); stage.setTitle("Particle game"); stage.setScene(gameWindow); //display game window stage.show(); } public static void main(String[] args) { launch(args); } }

我使用答案this question中的代码来尝试解决它,但我仍然遇到这些错误。我还在handleMovement方法中的代码周围使用了try-catch块,但它没有捕获任何东西,这使我认为错误是FXML或其他地方引起的?谁能解释为什么会这样,或者告诉我是否还有另一种方法可以监听鼠标的点击并确定用户在板上的点击位置?

对于Java等新手的强制性道歉。在此之后,我试图开发一个简单的游戏。目前,我要做的就是能够在板上移动计数器,但是...

java javafx javafx-8 gridpane
1个回答
2
投票
我想我可能已经找到了您的问题
© www.soinside.com 2019 - 2024. All rights reserved.