JavaFX OnMouseClick
在JavaFX开发环境中,遇到一些坑是难免的,而且资料少得可怜!
先说一下我遇到的问题 :
只是一个点击事件而已 :
首先我有这么个界面 :
接下来呢 ? 我需要点击右上角的X,然后显示遮罩,弹出对话框。
@FXML public void exitApp(ActionEvent event) { mask.setVisible(true); alertMessage.setVisible(true); }
XML控件:
<Pane fx:id="exitmask" layoutX="1146.0" layoutY="0.0" onMouseClicked="#exitApp" prefHeight="54.0" prefWidth="54.000099999997474" styleClass="hover-canlen"> <stylesheets> <URL value="@Menu.css" /> </stylesheets> </Pane>
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(FXMLLoader.java:1769) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470) at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398) at javafx.scene.Scene$MouseHandler.process(Scene.java:3766) at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431) at com.sun.glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyMouse(View.java:937) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177) at java.lang.Thread.run(Unknown Source)
编译器总是那么不留情面,还好我有百度 :
https://blog.csdn.net/strazhang/article/details/55281948
网上的答案:
改成onAction:
<Pane fx:id="exitmask" layoutX="1146.0" layoutY="0.0" onAction="#exitApp" prefHeight="54.0" prefWidth="54.000099999997474" styleClass="hover-canlen"> <stylesheets> <URL value="@Menu.css" /> </stylesheets> </Pane>
运行 :
javafx.fxml.LoadException: /D:/JavaProject/FX/OA/bin/oa/view/Menu.fxml:69 at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) at oa.main.MainApp.start(MainApp.java:21) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.UnsupportedOperationException: Cannot determine type for property. at com.sun.javafx.fxml.BeanAdapter.getSetterMethod(BeanAdapter.java:177) at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:250) at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:618) at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770) at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532) ... 17 more
最后找了半天 :
StackOverflow才是正解!
Controller里面对应点击事件的ActionEvent改成onMouseEvent!
这才说得过去嘛!
https://stackoverflow.com/questions/22935474/textfield-events-in-java-fx
Make sure the parameter type for processEnter(...)
is MouseEvent
, and that you have the correct import in the controller: javafx.scene.input.MouseEvent
, not the awt version.