2014-04-18 41 views
0
<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場景生成器,我想catch事件onMouseClicked或onMouseReleased,但我不明白該怎麼做。 這是錯誤,當我點擊切換按鈕:javafx新手切換按鈕事件

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) 

誰能舉個例子?在此先感謝

回答

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按鈕。使用鍵盤或觸摸怎麼樣?

0

我面臨同樣的錯誤以及&能夠解決。

異常在線程 「的JavaFX應用程序線程」 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公共無效loadDC1(ActionEvent的事件){} //方法