2014-09-03 41 views
0

我已經在JavaFX中創建了一個滾動窗格(帶有錨點窗格)。 在運行時我創建一個矩形並將其添加到ScrollPane。 我想在滾動窗格上添加一個鼠標單擊事件,其中我可以在運行時更改scrollPane的內容。在JavaFX中的ScrollPane上添加MouseClick事件

我想這樣做,但是當我點擊滾動窗格

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

回答

1

你所面臨的問題,是因爲該方法

@FXML  
void ChangeImageColor(ActionEvent event) 

的聲明,這裏的參數應該是MouseEvent型,而不是ActionEvent的。如果您不確定事件的類型,您也可以刪除該參數。嘗試使用:

@FXML  
void ChangeImageColor() 

@FXML  
void ChangeImageColor(MouseEvent event) 
+0

它的工作!謝謝! :) – AnkitG 2014-09-05 10:45:56

0

我不知道爲什麼在fxml中添加事件不起作用。 我只是在Java代碼中添加了事件定義,它工作得很好。

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