<ToggleButton fx:id="toggleDC1" layoutX="52.0" layoutY="146.0" mnemonicParsing="false" onMouseClicked="#loadDC1" selected="false" text="click">
<font>
<Font name="Aparajita" size="22.0" fx:id="x1" />
</font>
</ToggleButton>
我使用JavaFX Scene Builder,我想要捕捉事件onMouseClicked或onMouseReleased,但我不明白该怎么做。 单击togglebutton时出现错误:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1763)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651)
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.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:204)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3456)
at javafx.scene.Scene$ClickGenerator.access$8300(Scene.java:3387)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3755)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)
有人能举个例子吗?提前致谢
答案 0 :(得分:1)
切换按钮很奇怪。它们进入ToggleGroup
,但它们被单独添加到容器中。
final ToggleGroup tg = new ToggleGroup();
final ToggleButton tb1 = new ToggleButton("toggle 1");
tb1.setOnAction((ActionEvent e) -> {
System.out.println("toggle1");
});
final ToggleButton tb2 = new ToggleButton("toggle 2");
tb2.setOnAction((ActionEvent e) -> {
System.out.println("toggle2");
});
tg.getToggles().addAll(tb1,tb2);
hbox.getChildren().addAll(tb1,tb2);
我认为你不应该使用onMouseClicked按钮。如何使用键盘或触摸?
答案 1 :(得分:1)
我也面临同样的错误。能够解决。
线程“JavaFX Application Thread”中的异常java.lang.IllegalArgumentException:参数类型不匹配
以下是您的代码假设的样子
@FXML
public void loadDC1(ActionEvent event){
ToggleButton btnSelected = (ToggleButton) event.getSource();
String name=btnSelected.getId().toString();
if(name.equalsIgnoreCase("toggleDC1")){
//do something
}
}
在JavaFX中,您需要将fxml文件中的所有fx:id,#action定义为控制器类,如;
@FXML private ToggleButton toggleDC1; //作为类字段
@FXML public void loadDC1(ActionEvent event){} // method