我正在尝试创建一个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 :(得分:0)
两件事:
<Button fx:id="login" ... onAction="#handleButtonAction" ... >
(注意方法名称前的#
)和
fx:controller="..."
的控制器类。 (也许你是直接在代码中设置控制器 - 你没有显示加载FXML的位置 - 在这种情况下你不需要它。)答案 1 :(得分: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">
并在方法名称前添加#。
答案 2 :(得分: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">