gpt4 book ai didi

JavaFX .getResource 方法

转载 作者:行者123 更新时间:2023-12-02 04:45:31 25 4
gpt4 key购买 nike

我是 Java 新手,我在 Intellij 中不断收到错误,表明我的启动方法有问题。

我知道问题是因为它不喜欢该路径,但我将完整路径放入了login.fxml 文件中。我正在遵循教程,但路径正是我的路径,并且错误消失了。

这是我的代码:

package sample;

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

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/sample/view/login.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 700, 400));
primaryStage.show();
}


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

这也是我的 login.xml 文件代码:


<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="700.0" style="-fx-background-color: d32f2f;" xmlns="http://javafx.com/javafx/8.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.LoginController">
<children>
<AnchorPane layoutY="72.0" prefHeight="328.0" prefWidth="700.0" style="-fx-background-color: #ffffff;">
<children>
<JFXTextField fx:id="loginUserName" focusColor="#424242" layoutX="214.0" layoutY="67.0" prefHeight="27.0" prefWidth="272.0" promptText="Username" unFocusColor="#ff6659" />
<JFXPasswordField fx:id="loginPassword" focusColor="#424242" layoutX="214.0" layoutY="137.0" prefHeight="27.0" prefWidth="272.0" promptText="Password" unFocusColor="#ff6659" />
<Label alignment="CENTER" layoutX="285.0" layoutY="14.0" prefHeight="17.0" prefWidth="130.0" text="Login" textFill="#6f6767">
<font>
<Font size="18.0" />
</font>
</Label>
<JFXButton fx:id="loginButton" layoutX="269.0" layoutY="193.0" prefHeight="27.0" prefWidth="163.0" style="-fx-background-color: #424242;" text="Login" textFill="#fffefe" />
</children>
</AnchorPane>
<Label alignment="CENTER" layoutX="285.0" layoutY="14.0" prefHeight="17.0" prefWidth="130.0" text="TO DO" textFill="#fcfafa">
<font>
<Font size="36.0" />
</font>
</Label>
<JFXButton fx:id="loginSignUpButton" layoutX="598.0" layoutY="14.0" prefHeight="27.0" prefWidth="88.0" style="-fx-background-color: #9a0007;" text="Sign Up" textFill="WHITE" />
</children>
</AnchorPane>

这是我收到的错误:

"Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:945)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NoClassDefFoundError: com/sun/javafx/css/converters/PaintConverter
at com.jfoenix.controls.JFXTextField$StyleableProperties.<clinit>(JFXTextField.java:266)
at com.jfoenix.controls.JFXTextField.<init>(JFXTextField.java:171)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at java.base/java.lang.Class.newInstance(Class.java:558)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1019)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
at sample.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.ClassNotFoundException: com.sun.javafx.css.converters.PaintConverter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 25 more
Exception running application sample.Main
"

关于如何修复的任何想法?

最佳答案

您发布的这部分堆栈跟踪表明问题所在。

Caused by: java.lang.ClassNotFoundException: sample.controller.LoginController

来自ClassNotFoundExceptionjavadoc

Thrown when an application tries to load in a class through its string name using:

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader.
  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name could be found.

换句话说,找不到文件sample\controller\LoginController.class
请注意,上面的路径是相对路径,它是相对于您的应用程序的工作目录的。
工作目录是System属性“user.dir”的值,如

String workingDirectory = System.getProperty("user.dir");

这样您就可以检查为什么会出现异常。
要么没有这样的文件LoginController.class,要么它位于错误的目录中。

关于JavaFX .getResource 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56487335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com