带有 imageview-navigation 的 javafx

javafx with imageview-navigation

我在单击图像 (imageview) 导航到新屏幕时遇到问题。我已经编写了使用 ActionEvent 在屏幕之间切换的代码。现在我想在单击图像时浏览屏幕。我尝试做同样的事情,但它给出了类型不匹配错误。

我的 fxml 看起来像

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="299.0" prefWidth="439.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="multiplescreen.HomeScreenController">
    <children>
        <Button fx:id="button" layoutX="40.0" layoutY="245.0" onAction="#goToScreen1" text="Logout" />
      <Button layoutX="321.0" layoutY="245.0" mnemonicParsing="false" onAction="#Exit" text="Exit" />
      <Label layoutX="170.0" layoutY="30.0" text="Home Screen" />
      <ImageView id="project" fx:id="project" fitHeight="62.0" fitWidth="85.0" layoutX="31.0" layoutY="105.0" onMouseClicked="#goToProjectCreationScreen" pickOnBounds="true" preserveRatio="true">
         <!--<image>
            <Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/Project.png" />
         </image>-->
      </ImageView>
      <ImageView fitHeight="54.0" fitWidth="75.0" layoutX="127.0" layoutY="109.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/Execute.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="62.0" fitWidth="76.0" layoutX="212.0" layoutY="105.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/AccountInfo.jpg" />
         </image>
      </ImageView>
      <ImageView fitHeight="62.0" fitWidth="84.0" layoutX="307.0" layoutY="103.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/help.png" />
         </image>
      </ImageView>
      <Label layoutX="49.0" layoutY="173.0" text="Projects" />
      <Label layoutX="141.0" layoutY="173.0" text="Execute" />
      <Label layoutX="230.0" layoutY="173.0" text="Account Info" />
      <Label layoutX="336.0" layoutY="173.0" text="Help" />
    </children>
</AnchorPane>

我的控制器看起来像

public class HomeScreenController implements Initializable,ControlledScreen,EventHandler {

    ScreensController myController;
    @FXML
    private Label label;
     @FXML
    private ImageView project;

    @FXML
    private void goToScreen1(ActionEvent event) {
        myController.setScreen(MultipleScreen.screen1ID);
    }


  @FXML
    public void goToProjectCreationScreen(ActionEvent event) {
         Image image=new Image(getClass().getResourceAsStream("Project.png"));
        project.setImage(image);
        project.addEventFilter(javafx.scene.input.MouseEvent.MOUSE_CLICKED, new EventHandler<javafx.scene.input.MouseEvent>() {

            @Override
            public void handle(javafx.scene.input.MouseEvent event) {
                //To change body of generated methods, choose Tools | Templates.
                System.out.println("clicked");
                myController.setScreen(MultipleScreen.screen5ID);
                event.consume();
            }

        });  
        //myController.setScreen(MultipleScreen.screen5ID);
    }

    @FXML
       private void Exit(ActionEvent event) {
       Platform.exit();
    }



    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO

    }    

    @Override
    public void setScreenParent(ScreensController screenParent) {
       myController=screenParent;
    }

    @Override
    public void handle(Event event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

执行后,我在点击图片时遇到异常:

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:497)
                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:497)
                at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
                at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
                at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
                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:3471)
                at javafx.scene.Scene$ClickGenerator.access00(Scene.java:3399)
                at javafx.scene.Scene$MouseHandler.process(Scene.java:3767)
                at javafx.scene.Scene$MouseHandler.access00(Scene.java:3486)
                at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
                at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
                at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
                at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
                at java.security.AccessController.doPrivileged(Native Method)
                at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent0(GlassViewEventHandler.java:385)
                at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda1/20733739.get(Unknown Source)
                at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
                at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
                at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
                at com.sun.glass.ui.View.notifyMouse(View.java:927)
                at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
                at com.sun.glass.ui.win.WinApplication.lambda$null5(WinApplication.java:101)
                at com.sun.glass.ui.win.WinApplication$$Lambda/17230114.run(Unknown Source)
                at java.lang.Thread.run(Thread.java:745)

错误说明了一切。方法 goToProjectCreationScreen()onMouseClicked event, which expects a MouseEvent 上被调用。

所以该方法应该接受 MouseEvent而不是ActionEvent