2017-04-11 72 views
0

我想创建一个JavaFX程序,每次我想我的代码运行时我正在一个例外 - 我不完全知道这意味着什么,但...异常在应用程序启动方法在JavaFX项目

我的代码:

FXML文件

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

<?import com.jfoenix.controls.JFXPasswordField?> 
<?import com.jfoenix.controls.JFXTextField?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.Pane?> 
<?import javafx.scene.text.Font?> 

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Pane layoutX="49.0" layoutY="32.0" prefHeight="337.0" prefWidth="503.0"> 
     <children> 
      <JFXTextField fx:id="user" layoutX="45.0" layoutY="77.0" prefHeight="25.0" prefWidth="237.0" promptText="Username" /> 
      <JFXPasswordField fx:id="pass" layoutX="45.0" layoutY="126.0" prefHeight="25.0" prefWidth="237.0" promptText="Password" /> 
      <Button fx:id="login" layoutX="104.0" layoutY="211.0" mnemonicParsing="false" onAction="handleButtonAction" prefHeight="18.0" prefWidth="112.0" style="-fx-background-color: #2c50ba;" text="Login" textFill="#d1d4dd"> 
       <font> 
        <Font name="Andalus" size="19.0" /> 
       </font> 
      </Button> 
      <ImageView fitHeight="248.0" fitWidth="200.0" layoutX="289.0" layoutY="37.0" pickOnBounds="true" preserveRatio="true"> 
       <image> 
        <Image url="@Admin.png" /> 
       </image> 
      </ImageView> 
     </children> 
     </Pane> 
    </children> 
</AnchorPane> 

FXMLDocumentcotroller

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package projetoracle; 

import com.jfoenix.controls.JFXPasswordField; 
import com.jfoenix.controls.JFXTextField; 
import java.net.URL; 
import java.sql.Connection; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 


/** 
* 
* @author HP 
*/ 
public class FXMLDocumentController implements Initializable { 

    @FXML 
    private JFXTextField user; 

    @FXML 
    private JFXPasswordField pass; 

    @FXML 
    private Button login; 


    @FXML 
    private void handleButtonAction(ActionEvent event) { 
     String username = user.getText(); 
     String password = pass.getText(); 
     if (username.equals("System")&&password.equals("Manager")) 
     { 
     Connection con =connexion.getConnection(); 
     } 
     else 
     System.out.println("erreur"); 
    } 

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

} 

堆栈跟踪

Executing C:\Users\HP\Documents\NetBeansProjects\projetoracle\dist\run1189234243\projetoracle.jar using platform C:\Program Files\Java\jdk1.8.0_121\jre/bin/java 
Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    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:498) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    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:498) 
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: javafx.fxml.LoadException: Error resolving onAction='handleButtonAction', either the event handler is not in the Namespace or there is an error in the script. 
file:/C:/Users/HP/Documents/NetBeansProjects/projetoracle/dist/run1189234243/projetoracle.jar!/projetoracle/FXMLDocument.fxml:22 

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597) 
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103) 
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610) 
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770) 
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
    at projetoracle.Projetoracle.start(Projetoracle.java:22) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    ... 1 more 
Exception running application projetoracle.Projetoracle 
Java Result: 1 
Deleting directory C:\Users\HP\Documents\NetBeansProjects\projetoracle\dist\run1189234243 
jfxsa-run: 
BUILD SUCCESSFUL (total time: 6 seconds) 

我尝试这个话题给出的所有解决方案,但我不断收到同样的错误...

回答

0

两件事情:

  1. 你需要
<Button fx:id="login" ... onAction="#handleButtonAction" ... > 

(注意方法名称前的#)和

  1. 您似乎没有在FXML的根元素中指定控制器类fx:controller="..."。 (也许你是直接在代码中设置控制器 - 你没有显示你装载FXML的位置 - 在这种情况下,你将不需要这个。)
+0

当我把#放在方法名之前时,netbeans注意到关于控制器的错误:控制器没有在根组件中定义!请帮我修复它.. –

+0

@CyrineRebhi你没有读过我的答案中的第2项? –

+0

我读过它,但我不能定义控制器,你可以请完成这个FX:控制器=“...” –

0

请替换下面的代码片段;

@FXML 
private void handleButtonAction() { 

意味着删除参数和

<Button fx:id="login" layoutX="104.0" layoutY="211.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="18.0" prefWidth="112.0" style="-fx-background-color: #2c50ba;" text="Login" textFill="#d1d4dd"> 

和方法名前添加#。

+0

为什么要删除方法参数? –

+0

这似乎是无用的,其实并不重要。 – mtrgn

0

请替换下面的代码;

@FXML 
public void handleButtonAction() {} 

,你必须指定在FXML文件控制器。

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.sample.FXMLDocumentController "> 

与你的包名称替换com.sample.FXMLDocumentController

<Button fx:id="login" layoutX="104.0" layoutY="211.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="18.0" prefWidth="112.0" style="-fx-background-color: #2c50ba;" text="Login" textFill="#d1d4dd"> 
相关问题