java JavaFx 事件参数类型不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27079062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFx Event argument type mismatch
提问by eszik.k
I using JavaFx for first time and I encountered a problem with events. I built a form with SceneBuilder and I add an event on .fxml and controller but it's always return with:
我第一次使用 JavaFx,遇到了事件问题。我用 SceneBuilder 构建了一个表单,并在 .fxml 和控制器上添加了一个事件,但它总是返回:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
FirstScene.fxml:
FirstScene.fxml:
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="background" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="examlog.gui.FirstSceneController">
<stylesheets>
<URL value="@dark_style.css" />
</stylesheets>
<children>
<TableView fx:id="firstScene_tableView" layoutX="30.0" layoutY="42.0" prefHeight="287.0" prefWidth="600.0" AnchorPane.bottomAnchor="71.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="42.0">
<columns>
<TableColumn text="C1" />
<TableColumn prefWidth="70.0" text="C2" />
</columns>
</TableView>
<Label layoutX="14.0" layoutY="14.0" text="Létrehozott munkafolyamatok:" />
<Button fx:id="firstScene_newbutton" layoutX="522.0" layoutY="353.0" mnemonicParsing="false" onMouseClicked="#newButtonClick" text="úJ" />
</children>
</AnchorPane>
FirstSceneController.java:
FirstSceneController.java:
public class FirstSceneController implements Initializable {
@FXML
private Button firstScene_newbutton;
@FXML
private TableView firstScene_tableView;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void newButtonClick(ActionEvent event) {
System.out.println("You clicked me!");
}
}
JavaFxApplication.java:
JavaFxApplication.java:
public class JavaFXApplication extends Application {
private Stage stage;
@Override
public void start(Stage primaryStage) {
this.stage = primaryStage;
try {
FirstScene();
primaryStage.setTitle("FXML is Simple");
primaryStage.show();
} catch (IOException ex) {
Logger.getLogger(JavaFXApplication.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void FirstScene() throws IOException {
AnchorPane page = (AnchorPane) FXMLLoader.load(JavaFXApplication.class.getResource("FirstScene.fxml"));
Scene scene = new Scene(page);
stage.setScene(scene);
}
}
Exception:
例外:
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:1757)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
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:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3437)
at javafx.scene.Scene$ClickGenerator.access00(Scene.java:3365)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3733)
at javafx.scene.Scene$MouseHandler.access00(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null1(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda/14271771.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
I would really appreciate any help.
我真的很感激任何帮助。
回答by James_D
Use onAction="#newButtonClick"
, not onMouseClicked
.
使用onAction="#newButtonClick"
,不是onMouseClicked
。
回答by Alex Okrushko
Try using KeyEvent
instead of ActionEvent
尝试使用KeyEvent
代替ActionEvent
@FXML
private void newButtonClick(KeyEvent event) { ...}
回答by nickson makama
If you want to use a mouseClicked
event, simply change your newButtonClicked
method to take in MouseEvent
如果你想使用一个mouseClicked
事件,只需改变你的newButtonClicked
方法来接受MouseEvent
@FXML
private void newButtonClick(MouseEvent event){...}
回答by BENHISSI Youssef
instead of onMouseClicked you should use onAction
你应该使用 onAction 而不是 onMouseClicked