簡體   English   中英

如何修復 javafx.fxml.LoadException

[英]How to fix a javafx.fxml.LoadException

我正在查看 LoginDemo 的 javafx 示例代碼。 我使用了多場景應用的代碼; 我得到一個 javafx.fxml.LoadException。 我不知道該怎么辦。

我的代碼是打開一個窗口並有一個按鈕,然后單擊它以在 javafx TextArea 中顯示文本。

主要的:

package Test;

import java.io.InputStream;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

private Stage stage;

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

@Override
public void start(Stage primaryStage) throws Exception {
    try{
        stage = primaryStage;
        stage.setTitle("Test");
        setScene();
        stage.show();
    }catch(Exception e){
        e.printStackTrace();
    }

}

private void setScene(){
    try{
        Cont controller = (Cont) changeScene("Window.fxml");
        controller.setApp(this);
    }catch(Exception e){
        e.printStackTrace();
    }
}

private Initializable changeScene(String fxml) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    InputStream in = Main.class.getResourceAsStream(fxml);
    loader.setBuilderFactory(new JavaFXBuilderFactory());
    loader.setLocation(Main.class.getResource(fxml));
    AnchorPane page;
    try {
        page = (AnchorPane) loader.load(in);
    } finally {
        in.close();
    } 
    Scene scene = new Scene(page, 800, 600);
    stage.setScene(scene);
    stage.sizeToScene();
    return (Initializable) loader.getController();
   }

}

續:

package Test;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;


public class Cont implements Initializable {

private Main application;

@FXML
private Button btn;
@FXML
public TextArea Console;

public void setApp(Main application){
    this.application = application;
}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    assert btn != null : "fx:id=\"btn\" was not injected: check your FXML file 'Window.fxml'.";
    assert Console != null : "fx:id=\"Console\" was not injected: check your FXML file 'Window.fxml'.";

    btn.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent arg0) {
            setText("hello world");
        }
    });

}

public void setText(String text){
    Console.appendText(text+"\n");
 }

}

窗口.fxml:

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="Cont">
   <children>
      <TextArea fx:id="Console" prefHeight="365.0" prefWidth="600.0" />
      <Button fx:id="btn" layoutX="274.0" layoutY="374.0" mnemonicParsing="false" text="Button" />
   </children>
</AnchorPane>

錯誤:

javafx.fxml.LoadException: 
/C:/Users/user/workspace%20for%20coding/Javafx-Test/bin/Test/Window.fxml:9

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at Test.Main.changeScene(Main.java:49)
at Test.Main.setScene(Main.java:35)
at Test.Main.start(Main.java:25)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/70604542.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/455370116.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
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$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: Cont
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 24 more

fx:controller屬性需要控制器類的完全限定類名。 由於您的控制器Cont位於名為Test的包中,因此您需要

fx:controller="Test.Cont"

堆棧跟蹤確實為您提供了您需要的所有信息:錯誤來源的文件和行號:

/C:/Users/user/workspace%20for%20coding/Javafx-Test/bin/Test/Window.fxml:9

以及根本原因:

引起:java.lang.ClassNotFoundException:續

即它找不到名為Cont的類(因為它的名字是Test.Cont )。

(順便說一句,請注意包名稱應全部小寫,因此您確實應該調用包test ,而不是Test 。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM