2015-04-29 93 views
2

我正在用JavaFX製作一個桌面應用程序,並且我製作了一個只顯示5秒的啓動畫面,5秒鐘後它顯示一個帶有按鈕的新舞臺,當我點擊按鈕時發送一個錯誤:JavaFX初始屏幕按鈕onClick錯誤

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.access$8100(Scene.java:3399) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3767) 
at javafx.scene.Scene$MouseHandler.access$1500(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$handleMouseEvent$350(GlassViewEventHandler.java:385) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$158/361466793.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$null$145(WinApplication.java:101) 
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source) 
at java.lang.Thread.run(Thread.java:745) 

下面是JavaFX類

RootForm.java(閃屏)

package Forms; 

    import java.io.IOException; 
    import java.util.logging.Level; 
    import java.util.logging.Logger; 
    import javafx.animation.PauseTransition; 
    import javafx.application.Application; 
    import javafx.fxml.FXMLLoader; 
    import javafx.scene.Parent; 
    import javafx.scene.Scene; 
    import javafx.scene.image.Image; 
    import javafx.scene.paint.Color; 
    import javafx.stage.Stage; 
    import javafx.stage.StageStyle; 
    import javafx.util.Duration; 


    public class RootForm extends Application { 


@Override 
public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("RootForm.fxml")); 

    Scene scene = new Scene(root); 
    stage.setScene(scene); 
    stage.initStyle(StageStyle.TRANSPARENT); 
    stage.show(); 
    scene.setFill(Color.TRANSPARENT); 
    stage.getIcons().add(new Image("/Images/8-512.png")); 

    PauseTransition delay = new PauseTransition(Duration.seconds(5)); 
    delay.setOnFinished(event -> { 
     try { 
      Stage mainStage = new Stage(); 
      Parent parent = FXMLLoader.load(getClass().getResource("Error.fxml")); 
      mainStage.setScene(new Scene(parent)); 
      mainStage.show(); 
      mainStage.getIcons().add(new Image("/Images/8-512.png")); 
      stage.hide(); 
     } catch (IOException ex) { 
      Logger.getLogger(RootForm.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    }); 
    delay.play(); 
} 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    launch(args); 
} 

}

RootForm.fxml(閃屏FXML)

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

    <?import java.lang.*?> 
    <?import java.net.*?> 
    <?import java.util.*?> 
    <?import javafx.scene.co 

ntrol.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.paint.*?> 
<?import javafx.scene.text.*?> 

<AnchorPane id="Splash" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"> 
    <children> 
    <Label layoutX="57.0" layoutY="247.0" prefHeight="21.0" prefWidth="202.0" text="Software is loading..."> 
     <font> 
     <Font size="15.0" /> 
     </font> 
    </Label> 
    </children> 
    <stylesheets> 
    <URL value="@style.css" /> 
    </stylesheets> 
</AnchorPane> 

Error.fxml(第二階段與按鈕)

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

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

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="Forms.ErrorController"> 
    <children> 
    <Button fx:id="btnMain" onMouseClicked="#btnMainActEvent" layoutX="107.0" layoutY="65.0" mnemonicParsing="false" prefHeight="112.0" prefWidth="101.0" text="Button" textFill="RED" /> 
    </children> 
    <stylesheets> 
    <URL value="@style.css" /> 
    </stylesheets> 
</AnchorPane> 

ErrorController.java(第二階段應出現的)

package Forms; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 

/** 
* FXML Controller class 
* 
* @author User 
*/ 
public class ErrorController implements Initializable { 

    @FXML 
    private Button btnMain; 

    public void btnMainActEvent(ActionEvent event) throws Exception{ 

     System.out.println("U Shtyp!"); 

    } 
    /** 
    * Initializes the controller class. 
    */ 
    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     // TODO 
    }  

} 

下面是截圖: Splash Screen

Second Screen with The Button

當我單擊第二個屏幕上的按鈕後,我得到該錯誤。 任何人都可以幫我處理這個問題嗎?

+0

是否確定按鈕點擊處理程序中的ActionEvent?嘗試使用javafx.scene.input.MouseEvent(你不能將MouseEvent投射到ActionEvent) – mrak

回答

3

問題出在您在fxml中使用的onMouseClicked。它發送一個MouseEvent而不是ActionEvent

您需要在fxml中使用onAction才能設置要在按鈕的fire (click) event上執行的操作。

<Button fx:id="btnMain" onAction="#btnMainActEvent" layoutX="107.0" layoutY="65.0" mnemonicParsing="false" prefHeight="112.0" prefWidth="101.0" text="Button" textFill="RED" /> 
+0

謝謝拍賣:) – codeDevil