如何在我的 GridPane 上使用 onMouseClicked 修复 'Illegal Argument Exception'?

How do fix 'Illegal Argument Exception' using onMouseClicked on my GridPane?

对 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.access00(Scene.java:3398)
[java]  at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
[java]  at javafx.scene.Scene$MouseHandler.access00(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(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(WinApplication.java:186)
[java]  at java.lang.Thread.run(Thread.java:748)

我的控制器class:

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>

我的主要class:

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);
    }

}

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

我想我可能已经发现了你的问题

@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());
}

我注意到您使用 handleMovement() 作为 GridPane 的 onMouseClicked 属性的值。如此处所示:

<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">

我认为问题可能在于 handleMovement 函数需要一个 MouseEvent 类型的参数而不是 ActionEvent.

所以您的 handleMovement() 函数现在应该如下所示:

@FXML
private void handleMovement(MouseEvent 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());
}

这可以解释 java.lang.IllegalArgumentException: argument type mismatch 错误。 JavaFX 期待一个以 MouseEvent 作为唯一参数的函数,但它得到的是一个具有 ActionEvent 类型的函数。

我无法让它工作,所以决定使用 Swing 而不是 JavaFX 重写程序。