我无法弄清楚这段代码的问题。我在这里研究了许多类似的问题,解决了目录是否正确,可能是错误的函数调用等问题。
我希望有人可以帮助我。一切都在名为loginapp的应用程序中名为login的文件中。
这是Login.java
/*
* 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 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 {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Login.fxml"));
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Fracken");
stage.show();
}
}
这是Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="317.0" prefWidth="326.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.Login">
<children>
<TextField fx:id="txtUsername" layoutX="110.0" layoutY="45.0" promptText="Username" />
<PasswordField fx:id="txtPassword" layoutX="110.0" layoutY="115.0" promptText="Password" />
<Button fx:id="btnLogin" layoutX="110.0" layoutY="184.0" mnemonicParsing="false" onAction="btnLoginAction" text="Login" />
<Button fx:id="btnReset" layoutX="232.0" layoutY="184.0" mnemonicParsing="false" onAction="btnResetAction" text="Reset" />
<Label fx:id="lblMessage" layoutX="110.0" layoutY="236.0" prefHeight="31.0" prefWidth="187.0" />
</children>
</AnchorPane>
我确定问题出在
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Login.fxml"));
我收到此错误。
Executing C:\Users\David\Desktop\Java Project\loginapp\dist\run122343396\loginapp.jar using platform C:\Program Files\Java\jdk1.8.0_111\jre/bin/java
Exception in Application start method
Exception in thread "main" 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 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: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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 login.Login.start(Login.java:23)
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
Java Result: 1
请帮助我,我在这里看了其他类似的问题,但没有解决方案有效。
答案 0 :(得分:3)
getClass().getResource("Login.fxml")
应该返回非空值,因为使用Class.getResource
查找相对于类的资源。由于您似乎已将资源放在login
包中,因此使用资源名称为Login.fxml
的包中的类应该可以正常工作。
出于某种原因,您不使用通过此资源FXMLLoader
的{{1}}。而是使用
URL
即。您使用类加载器来加载文件。然而,类加载器不知道您从中检索它的类,因此尝试在默认包中找到getClass().getClassLoader().getResource("Login.fxml")
。
如果使用类加载器,则应使用完整路径,即
Login.fxml
答案 1 :(得分:1)
我的fxml文件与main(不在同一个包)上的文件夹不在同一个文件夹中。我得到的唯一方法是使用:getClass()。getResource(“/ fxml / inicial.fxml”);
答案 2 :(得分:0)
您应该描述:fx:controller =“ login.Login” 表单Login.fxml
答案 3 :(得分:-2)
只需添加用户库。来自java-jre-lib-javafx的javafx。