我在JavaFX中创建了一个Scroll Pane(带有Anchor Pane)。 在运行时,我正在创建一个矩形并将其添加到ScrollPane。 我想在滚动窗格上添加鼠标单击事件,我可以在运行时更改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);
答案 0 :(得分:1)
您遇到的问题是由于声明了方法
@FXML
void ChangeImageColor(ActionEvent event)
此处参数的类型应为MouseEvent
,而不是ActionEvent
。如果您不确定它是什么类型的事件,也可以删除该参数。尝试使用:
@FXML
void ChangeImageColor()
或
@FXML
void ChangeImageColor(MouseEvent event)
答案 1 :(得分:0)
我不确定为什么在fxml中添加事件不起作用。 我只是在Java代码中添加了事件定义,它工作得很好。
scrollPane.setOnMouseClicked(new EventHandler<Event>()
{
@Override
public void handle(Event event)
{
//Logic on event occurrence
}
});