我是javafx的新手并尝试使用javafx创建一个功能登录应用程序。当我通过netbeans为javafx fxml应用程序运行默认代码时,它运行得很好但是当我使用场景构建器和代码简单逻辑创建我自己的应用程序时,它会给出错误。
这是错误
Executing C:\Users\Ovonel\Documents\NetBeansProjects\Digi_Library\dist\run1053353439\Digi_Library.jar using platform C:\Program Files (x86)\Java\jdk1.8.0_172\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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/Ovonel/Documents/NetBeansProjects/Digi_Library/dist/run1053353439/Digi_Library.jar!/digi_library/login/Login.fxml:11
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
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 digi_library.login.Login.start(Login.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.NullPointerException
at digi_library.login.LoginController.<init>(LoginController.java:17)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 17 more
Exception running application digi_library.login.Login
Java Result: 1
这是控制器:
package digi_library.login;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javax.swing.JOptionPane;
public class LoginController implements Initializable {
private TextField usernameTF;
private PasswordField passwordTF;
String username = usernameTF.getText();
String password = passwordTF.getText();
// Legit User Details
String uname = "adming";
String pass = "root";
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
private void Login(ActionEvent event)
{
try
{
if(username.equals(uname) && password.equals(pass))
{
JOptionPane.showMessageDialog(null, "Legit user, login in!");
}
else if(username.isEmpty())
{
JOptionPane.showMessageDialog(null,"Enter username");
}
else if(password.isEmpty())
{
JOptionPane.showMessageDialog(null,"Enter password");
}
else if(username.isEmpty() && password.isEmpty())
{
JOptionPane.showMessageDialog(null,"Enter username as well as password to login");
}
else
{
JOptionPane.showMessageDialog(null, "Access Denied!");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
private void Cancel(ActionEvent event)
{
System.exit(0);
}
}
这是Fxml文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="digi_library.login.LoginController">
<children>
<PasswordField layoutX="100.0" layoutY="180.0" prefHeight="25.0" prefWidth="200.0" promptText="Password" />
<TextField layoutX="100.0" layoutY="115.0" prefHeight="25.0" prefWidth="200.0" promptText="Username" />
<ImageView fitHeight="40.0" fitWidth="40.0" layoutX="180.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../resources/icons/icons8_Lock_100px.png" />
</image>
</ImageView>
<Button layoutX="50.0" layoutY="256.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" text="Login" />
<Button layoutX="250.0" layoutY="256.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" text="Cancel" />
<ImageView fitHeight="20.0" fitWidth="20.0" layoutX="65.0" layoutY="118.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../resources/icons/icons8_User_24px.png" />
</image>
</ImageView>
<ImageView fitHeight="20.0" fitWidth="20.0" layoutX="65.0" layoutY="183.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../resources/icons/icons8_Password_1_24px.png" />
</image>
</ImageView>
</children>
</Pane>
这是主要的java文件
package digi_library.login;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Login extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我在Scene Builder中尝试了fx:root选项,但它没有用。当我将jfoenix.jar用于我的项目时,也会出现此错误。任何帮助将不胜感激。