gpt4 book ai didi

start 方法中的 JavaFX InvokingTargetException

转载 作者:行者123 更新时间:2023-12-02 11:35:19 24 4
gpt4 key购买 nike

我正在创建一个简单的 JavaFX 应用程序,它抛出 InitationTargetException

当我尝试加载 FXML 文件时,引发的异常与路径规范中的错误有关;因此,为了确保这确实是异常背后的原因,我尝试创建一个简单的 JavaFX 应用程序,在场景中显示一个按钮。不管怎样,都会抛出相同的异常。

我发现了一些与此相关的已解决问题,并且我尝试了所有解决方案,例如删除
fxml 文件路径中的\并创建 FXMLLoader 实例,而不是依赖于静态 load(...) 方法。这些都不适合我。

主类:

 package UI;

import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class MainFX extends Application {

private Stage stage;
private BorderPane borderPane;
@Override
public void start(Stage stage) throws IOException {
this.stage = stage;
this.stage.setTitle("App");
showMainView();
}
private void showMainView() throws IOException
{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainFX.class.getResource("login.fxml"));
borderPane = loader.load();
Scene scene = new Scene(borderPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}

登录.fxml:

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

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXSpinner?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.effect.Light.Distant?>
<?import javafx.scene.effect.Lighting?>
<?import javafx.scene.effect.Shadow?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="640.0" style="-fx-background-color: #222629;" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
<center>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="208.0" minWidth="10.0" prefWidth="166.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="414.0" minWidth="10.0" prefWidth="266.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="168.0" minWidth="10.0" prefWidth="168.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<JFXTextField focusColor="#999ea1" promptText="Email" style="-fx-text-inner-color: #999ea1;" unFocusColor="#474b4f" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<JFXPasswordField focusColor="#999ea1" labelFloat="true" promptText="Password" style="-fx-text-inner-color: #999ea1;" unFocusColor="#474b4f" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Text fill="WHITE" lineSpacing="50.0" pickOnBounds="true" smooth="false" strokeWidth="0.0" text="Login" textAlignment="CENTER" wrappingWidth="265.443359375" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font name="Arial Bold" size="34.0" />
</font>
</Text>
<GridPane GridPane.columnIndex="1" GridPane.rowIndex="7">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<JFXButton alignment="TOP_CENTER" prefHeight="25.0" prefWidth="135.0" ripplerFill="#979797" text="Cancel" textFill="WHITE" />
<JFXButton alignment="TOP_CENTER" prefHeight="25.0" prefWidth="137.0" ripplerFill="#86c232" style="-fx-background-color: #61892F;" text="Login" textFill="WHITE" GridPane.columnIndex="1" />
</children>
</GridPane>
<JFXSpinner prefHeight="22.0" prefWidth="198.0" GridPane.columnIndex="1" GridPane.rowIndex="6">
<effect>
<Lighting>
<bumpInput>
<Shadow />
</bumpInput>
<light>
<Light.Distant />
</light>
</Lighting>
</effect>
</JFXSpinner>
</children>
</GridPane>
</center>
</BorderPane>

堆栈跟踪:

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/User/Documents/NetBeansProjects/ JavaFXApplication2/dist/run966830135/JavaFXApplication2.jar!/login.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
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 Main.start(Main.java:21)
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.ClassNotFoundException: com.jfoenix.controls.JFXButton
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 20 more
Exception running application Main
Java Result: 1

最佳答案

您已将 Jfoenix API 导入到场景构建器中,但未将其包含到项目的构建路径中。

将 JAR 文件添加到项目的确切方法取决于您的 IDE。您可以快速谷歌一下并自行找到答案。

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

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