首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FXML ClassNotFoundException

FXML ClassNotFoundException
EN

Stack Overflow用户
提问于 2016-03-25 16:32:07
回答 2查看 6.1K关注 0票数 0

错误

代码语言:javascript
复制
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通过查找.令牌和后面的标识符来解析导入语句。所以我把它作为包的一部分,我仍然收到同样的错误。

FXMLEx.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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" />
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-25 19:33:24

首先,您应该检查是否可以加载FXML文件。getClass().getResource("FXMLDoc.fxml")必须不是null,并且不能提供任何例外。

第二,正如@fabian已经说过的,您的FXML格式不正确。它必须与任何其他一样只包含一个根标记。

票数 2
EN

Stack Overflow用户

发布于 2016-03-25 19:46:42

您的FXML格式无效。您应该在GridPane容器中拥有所有节点。

正如法比安在评论中所说,确保有一个单一的根元素。

试着将代码简单地放在下面,看看应用程序是否正在运行。如果是,那么它被错误地定义了fxml文件布局。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="377.0" prefWidth="533.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="fxmltut.FXMLCont">

</GridPane>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36223978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档