【问题标题】:Add MouseClick Event on ScrollPane in JavaFX在 JavaFX 中的 ScrollPane 上添加 MouseClick 事件
【发布时间】:2014-09-03 17:57:15
【问题描述】:

我在 JavaFX 中创建了一个滚动窗格(带有锚窗格)。 在运行时,我正在创建一个矩形并将其添加到 ScrollPane。 我想在滚动窗格上添加一个鼠标单击事件,在其中我可以在运行时更改滚动窗格的内容。

我尝试这样做,但是当我单击 ScrollPane 时出现此异常

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
at javafx.scene.Scene$ClickGenerator.access$7900(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

这是我的 JavaFX fxml 部分,

<ScrollPane fx:id="scrollPane" layoutX="229.0" layoutY="183.0" onMouseClicked="#ChangeImageColor" prefHeight="137.0" prefWidth="143.0">
       <content>
           <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="154.0" prefWidth="157.0" />
       </content>
</ScrollPane>

还有我的 Java 代码,

@FXML
void ChangeImageColor(ActionEvent event)
{
    System.out.print("Hit!");
    scrollPane.setContent(null);
    Rectangle rectangle = new Rectangle(200, 200, Color.BLUEVIOLET);
    rectangle.setStroke(Color.BLACK);
    rectangle.setStrokeWidth(25);
    scrollPane.setContent(rectangle);           
}

这就是我在初始化时所做的,

Rectangle rectangle = new Rectangle(200, 200, Color.RED);
rectangle.setStroke(Color.BLACK);
rectangle.setStrokeWidth(25);
scrollPane.setContent(rectangle);

【问题讨论】:

    标签: events javafx scrollpane onmouseclick


    【解决方案1】:

    你面临的问题是因为方法的声明

    @FXML    
    void ChangeImageColor(ActionEvent event)
    

    这里参数的类型应该是MouseEvent,而不是ActionEvent。如果您不确定它是什么类型的事件,您也可以删除该参数。尝试使用:

    @FXML    
    void ChangeImageColor()
    

    @FXML    
    void ChangeImageColor(MouseEvent event)
    

    【讨论】:

      【解决方案2】:

      我不确定为什么在 fxml 中添加事件不起作用。 我只是在 Java 代码中添加了事件定义,它工作得非常好。

      scrollPane.setOnMouseClicked(new EventHandler<Event>() 
      {
          @Override
          public void handle(Event event)
          {
               //Logic on event occurrence
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-23
        • 2012-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多