java 如何修复 javafx.fxml.LoadException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31683911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix a javafx.fxml.LoadException
提问by TheGathron
I'm looking at the javafx example code the LoginDemo. I used the code for multiply scene application; and I get a javafx.fxml.LoadException. I don't know what to do.
我正在查看 LoginDemo 的 javafx 示例代码。我使用了多场景应用的代码;我得到一个 javafx.fxml.LoadException。我不知道该怎么办。
My code is to open a window and have a button and click it to display text in a javafx TextArea.
我的代码是打开一个窗口并有一个按钮,然后单击它以在 javafx TextArea 中显示文本。
Main:
主要的:
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();
}
}
Cont:
续:
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");
}
}
Window.fxml:
窗口.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>
The Error:
错误:
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.access0(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$launchApplication19(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda/70604542.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null0(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/455370116.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater1(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda/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$null5(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda/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
回答by James_D
The fx:controller
attribute requires the fully qualified class name of the controller class. Since your controller Cont
is in a package called Test
, you need
该fx:controller
属性需要控制器类的完全限定类名。由于您的控制器Cont
位于名为 的包中Test
,因此您需要
fx:controller="Test.Cont"
The stack trace really gives you all the information you need here: the file and line number where the error originates:
堆栈跟踪确实为您提供了您需要的所有信息:错误来源的文件和行号:
/C:/Users/user/workspace%20for%20coding/Javafx-Test/bin/Test/Window.fxml:9
/C:/Users/user/workspace%20for%20coding/Javafx-Test/bin/Test/Window.fxml:9
and the underlying cause:
以及根本原因:
Caused by: java.lang.ClassNotFoundException: Cont
引起:java.lang.ClassNotFoundException:续
i.e. it can't find the class called Cont
(because its name is Test.Cont
).
即它找不到被调用的类Cont
(因为它的名字是Test.Cont
)。
(As an aside, note that package names should be all lower case, so you really should call the package test
, not Test
.)
(顺便说一句,请注意包名称应全部小写,因此您确实应该调用 package test
,而不是Test
。)