当前位置: 首页 > 知识库问答 >
问题:

如何使用onMouseClicked on my GridPane修复“非法参数异常”?

贺佑运
2023-03-14

对于不熟悉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);
    }

}

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

共有2个答案

欧阳山
2023-03-14

我无法使其正常工作,因此决定使用Swing而不是JavaFX重新编写程序。

仉俊能
2023-03-14

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

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

我注意到您正在使用handleMobile()作为GridPane的onMouseClick属性的值。如图所示:

<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函数需要一个类型为MouseeEvent的参数,而不是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:参数类型不匹配错误。JavaFX期望得到一个MouseEvent作为唯一参数的函数,但它得到的却是一个ActionEvent类型的函数。

 类似资料:
  • 我正在处理一个非常简单的point类,但我得到了一个错误,我无法确定字符串/双值问题发生的位置或如何修复它。 编辑 我忘记添加我正在接收的错误:

  • 问题内容: 我正在Ubuntu 16.04上使用最新版本的Elasticsearch,但在将数据放到上面时遇到了一个小问题。 这是我的json文档(相关部分) 这是当我尝试“ PUT http:// localhost:9200 / aws ” 时从ES返回的响应 在我看来,ES认为“ clockSpeed”是某种设置…?我希望使用动态映射来加快此过程,而不是先映射所有文档,然后将其导入ES。 有

  • 在Java jar上运行JUnit5测试并加载依赖项时,会出现警告 当我去看dumpstream时,它充满了评论,比如: 解决了依赖项加载问题,但未解决损坏的流。

  • 问题内容: 我正在尝试将XML发送到服务器并取回XML。有什么办法可以解决/忽略此异常吗? 我知道状态行为空,这会引发此错误。 问题答案: 尝试看看您的服务器实际上返回了什么!它可能不是有效的HTTP响应。您可以使用以下方式将原始的http请求发送到服务器: 响应应类似于:

  • 问题内容: 我正在编写一个简单的程序,该程序使用读取和处理文件内容。 但是当我第二秒钟调用另一个文件名时,出现以下异常: 线程“主”中的异常java.io.IOException:流已关闭 我不知道如何关闭流。我犯了什么错误,该如何解决? 问题答案: 由于您要先关闭流,所以流已关闭 读取文件名后发出的问题。 不要关闭该阅读器,也不要为其创建新的阅读器-只需重用该阅读器即可。但是,请使用其他文件读取

  • 我试图使用Mockito来模拟一个方法,但是,我不断地出错。我试图用spring注释设置Mockito @RunTo(MockitoJUnitRunner.class)类CPEServiceInvokerInimplNewTest{ 然后我用这个Mockito调用- 尝试并模仿这种方法- 我得到了这个错误- 如果匹配器与原始值组合,则可能会发生此异常://不正确:somethod(anyObjec