2017-02-09 3 views
0

Je suis nouveau sur javafx et j'essayais de créer un gui à partir duquel on cliquait sur un bouton, il passait à une autre fenêtre. J'ai essayé de lire beaucoup de réponses trouvées sur Stack Overflow. J'ai même essayé de refaire le projet depuis le début ... Mais je continue d'avoir la même erreur.Exception dans la méthode de démarrage de l'application

C'est l'exception est la méthode de démarrage de l'application java.lang.reflect.InvocationTargetException de JavaFX

Mon principal

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class SkyTravelsFx extends Application { 

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

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

} 

LoginController.java

package skytravelsfx; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class LoginController implements Initializable { 

    @FXML 
    TextField username; 

    @FXML 
    TextField password; 

    @FXML 
    Button user; 

    @FXML 
    Button admin; 

    @FXML 
    Button login; 

    @FXML 
    public void btnClicked (ActionEvent actionEvent){ 
     try{ 
      FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("User.fxml")); 
      Parent root=(Parent) fxmlLoader.load(); 
      Stage stage = new Stage(); 
      Scene scene = new Scene(root); 
      stage.setScene(scene); 
      stage.initStyle(StageStyle.UNDECORATED); 
      stage.show(); 
      ((Stage)(((Button)actionEvent.getSource()).getScene().getWindow())).hide(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 
} 

S'il vous plaît noter que je toujours havre de paix » t écrit des codes dans UserController.java Je viens de faire l'interface utilisateur pour User.fxml

Login.fxml

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

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

<AnchorPane id="AnchorPane" prefHeight="766.0" prefWidth="1366.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="skytravelsfx.LoginController"> 
    <children> 
     <ImageView fitHeight="866.0" fitWidth="1397.0" layoutX="10.0" layoutY="11.0" pickOnBounds="true" preserveRatio="true"> 
     <image> 
      <Image url="@../../../SkyTravelFx/src/goodwp.com_25168.jpg" /> 
     </image> 
     </ImageView> 
     <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /> 
     <Label fx:id="label1" layoutX="565.0" layoutY="517.0" minHeight="16" minWidth="69" text="Select User"> 
     <font> 
      <Font size="18.0" /> 
     </font> 
     </Label> 
     <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#095a77" height="140.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="1386.0" /> 
     <Label fx:id="label11" layoutX="56.0" layoutY="33.0" minHeight="16" minWidth="69" text="Sky Travels" textFill="WHITE"> 
     <font> 
      <Font name="System Italic" size="72.0" /> 
     </font> 
     </Label> 
     <Label fx:id="label12" layoutX="432.0" layoutY="91.0" minHeight="16" minWidth="69" text="Making your destination closer..." textFill="WHITE"> 
     <font> 
      <Font name="System Italic" size="18.0" /> 
     </font> 
     </Label> 
     <AnchorPane layoutX="83.0" layoutY="252.0" prefHeight="507.0" prefWidth="383.0"> 
     <children> 
      <ImageView fitHeight="191.0" fitWidth="234.0" layoutX="96.0" layoutY="6.0" pickOnBounds="true" preserveRatio="true"> 
       <image> 
        <Image url="@../../../SkyTravelFx/src/icon-default-profile.png" /> 
       </image> 
      </ImageView> 
       <Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#handleButtonAction" style="-fx-background-color: #095a77;" text="Log In" textFill="WHITE"> 
       <font> 
        <Font size="18.0" /> 
       </font> 
      </Button> 
      <Label fx:id="label2" layoutX="152.0" layoutY="308.0" minHeight="16" minWidth="69" text="Password"> 
       <font> 
        <Font size="18.0" /> 
       </font> 
      </Label> 
      <Label fx:id="label2" layoutX="150.0" layoutY="214.0" minHeight="16" minWidth="69" text="Username"> 
       <font> 
        <Font size="18.0" /> 
       </font> 
      </Label> 
      <TextField fx:id="username" layoutX="115.0" layoutY="241.0" /> 
      <TextField fx:id="password" layoutX="115.0" layoutY="335.0" /> 
     </children> 
     </AnchorPane> 
     <Button fx:id="admin" layoutX="621.0" layoutY="592.0" mnemonicParsing="false" style="-fx-background-color: #095a77;" text="Log In as Admin" textFill="WHITE"> 
     <font> 
      <Font size="18.0" /> 
     </font> 
     </Button> 
     <Button fx:id="user" layoutX="621.0" layoutY="675.0" mnemonicParsing="false" onAction="#btnClicked" prefHeight="39.0" prefWidth="153.0" style="-fx-background-color: #095a77;" text="Log In as User" textFill="WHITE"> 
     <font> 
      <Font size="18.0" /> 
     </font> 
     </Button> 
    </children> 
</AnchorPane> 

Voici l'erreur d'exécution fois que je reçois.

Exception en mode de démarrage de l'application java.lang.reflect.InvocationTargetException à sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) à sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) à sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) à java.lang.reflect.Method.invoke (Method.java:498) à com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs (LauncherImpl.java : 389) at com.sun.javafx.application.LauncherImpl.launchApplication (LauncherI mpl.java:328) à sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) à sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) à sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java: 43) à java.lang.reflect.Method.invoke (Method.java:498) à sun.launcher.LauncherHelper $ FXHelper.main (LauncherHelper.java:767) Causé par: java.lang.RuntimeException: Exception dans démarrage de l'application méthode à com.sun.javafx.application.LauncherImpl.launchApplication1 (LauncherImpl.java:917) à com.sun.javafx.application.LauncherImpl.lambda $ launchApplication 155 $ (LauncherImpl.java:182) at java.lang.Thread.run (Thread.java:745) Causé par: javafx.fxml.LoadException: Erreur lors de la résolution onAction = '# handleButtonAction', soit le gestionnaire d'événements ne se trouve pas dans l'espace de noms , soit il existe un erreur dans le script. fichier :/C: /Users/Dell/Documents/NetBeansProjects/SkyTravelsFx/dist/run1245920268/SkyTravelsFx.jar /skytravelsfx/Login.fxml: 47

à javafx.fxml.FXMLLoader.constructLoadException (FXMLLoader.java : 2597) à javafx.fxml.FXMLLoader.access $ 100 (FXMLLoader.java:103) à javafx.fxml.FXMLLoader $ Element.processEventHandlerAttributes (FXMLLoader.java:610) à javafx.fxml.FXMLLoader $ ValueElement.processEndElement (FXMLLoader.java:770) à javafx.fxml.FXMLLoader.processEndElement (FXMLLoader.java:2823) à javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java: 2532) à javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:2441) à javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:3214) à javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java: 3175) à javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:3148) à javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:3124) à javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:3104) à javafx.fxml.FXMLLoader.load (FXMLLoader.java:3097) à skytravelsfx.SkyTravelsFx.start (SkyTravelsFx.java:23) à com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 162 $ (LauncherImpl.java : 863) à com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175 (PlatformImpl.j ava: 326) à com.sun.javafx.application.PlatformImpl.lambda null $ $ 173 (PlatformImpl.java:295) à java.security.AccessController.doPrivileged (Native Method) à de com.sun.javafx.application .PlatformImpl.lambda $ runLater $ 174 (PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher $ Future.run (InvokeLaterDispatcher.java:95) à l'adresse com.sun.glass.ui.win.WinApplication ._runLoop (Méthode native) à com.sun.glass.ui.win.WinApplication.lambda $ null $ 148 (WinApplication.java:191) ... 1 plus Exception application en cours d'exécution skytravelsfx.SkyTravelsFx Java Résultat: 1 Suppression du répertoire C: \ Utilisateurs \ Dell \ Documents \ NetBeansProjects \ SkyTravelsFx \ dist \ run1245920268 jfxsa-run: CONSTRUIRE SUCCES (durée totale: 4 secondes)

Répondre

2

La trace de pile vous indique le problème:

Erreur de résolution onAction='#handleButtonAction', soit le gestionnaire d'événements ne sont pas dans l'espace ou il y a une erreur dans le script.

Vous avez

<Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#handleButtonAction" ... > 

mais votre contrôleur n'a pas de méthode appelée handleButtonAction. Vous voulez probablement

<Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#btnClicked" ... > 
+0

merci James_D de me l'avoir montré. je suis nouveau à java fx et a été perdu à la recherche d'une réponse qui correspond à la mienne. –