gpt4 book ai didi

java - FXML 类未找到异常

转载 作者:行者123 更新时间:2023-12-01 18:06:15 25 4
gpt4 key购买 nike

错误

C:\Users\rbenedict\Documents\Java Modules>java -cp .;fxmltut fxmltut.FXMLEx
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unk
nown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Sou
rce)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So
urce)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(
Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/rbenedict/Documents/Java%20Modules/fxmltut/FXMLDoc.fxml

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.importClass(Unknown Source)
at javafx.fxml.FXMLLoader.processImport(Unknown Source)
at javafx.fxml.FXMLLoader.processProcessingInstruction(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at fxmltut.FXMLEx.start(FXMLEx.java:17)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162
(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown
Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Sourc
e)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown S
ource)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
... 1 more
Caused by: java.lang.ClassNotFoundException
at javafx.fxml.FXMLLoader.loadType(Unknown Source)
... 21 more
Exception running application fxmltut.FXMLEx

我尝试过使用和不使用包fxmltut。如果没有,我也会遇到同样的异常,并在网上做了一些检查。显然,javafx 通过查找 . 标记并在后面获取标识符来解析 import 语句。所以我将其作为包的一部分,但仍然遇到相同的错误。

FXMLEx.java

package fxmltut;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

import javafx.scene.Parent;
import java.lang.Class;

import javafx.fxml.FXMLLoader;

public class FXMLEx extends Application {

@Override
public void start(Stage stage) throws Exception {

Parent root = FXMLLoader.load(getClass().getResource("FXMLDoc.fxml"));
Scene scene = new Scene(root, 300,275);

stage.setTitle("FXML Welcome");
stage.setScene(scene);
stage.show();

}

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

}

FXMLCont.java

package fxmltut;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;

public class FXMLCont {
@FXML private Text actTarg;
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
actTarg.setText("Sign in button pressed");
}
}

FXMLDoc.fxml

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

<?import fxmltut.*?>
<?import java.net.*?>
<?import java.geometry.*?>
<?import java.scene.control*?>
<?import java.scene.layout.*?>
<?import java.scene.text.*?>

<GridPane fx:controller="fxmltut.FXMLCont"

xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="25" left="25"/></padding>

</GridPane>

<Text text="Welcome"
GridePane.columnIndex="0" GridPane.rowIndex="0"
GridPane.columnSpan="2"/>

<Label text="User Name:"
GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField
GridPane.columnIndex="1" GridPane.rowIndex="1"/>

<Label text="Password:"
GridPane.columnIndex="0" GridPane.rowIndex="2"/>

<PasswordField fx:id="pw" GridPane.columnIndex="1" GridPane.rowIndex="2"/>

<HBox spacing="10" alignment = "bottom_right"
GridPane.columnIndex="1" GridPane.rowIndex="4">

<Button text="Sign in"
onAction="#handleSubmitButtonAction"/>

</HBox>

<Text fx:id="actTarg"
GridPane.columnIndex="0" GridPane.columnSpan="2"
GridPane.halignment="RIGHT" GridPane.rowIndex="6" />

最佳答案

首先您应该检查 FXML 文件是否可以加载。 getClass().getResource("FXMLDoc.fxml") 必须不为 null,并且不得提供异常。

其次,正如 @fabian 所说,您的 FXML 格式不正确。与任何其他 XML 一样,它只能包含一个标记。

关于java - FXML 类未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36223978/

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