我已经阅读了所有旧问题,但我不明白发生了什么。 只有当我尝试将文件layout.fxml与控件类连接时才会出现问题。 我试过1.000路径,但它不起作用...... 我需要你的帮助,抱歉这个愚蠢的问题:(
错误:
> 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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: /Users/claudio/Documents/workspace/KlickiBunti/Layout.fxml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at java.net.URL.openStream(URL.java:1045)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2440)
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 control.Main_Controller.start(Main_Controller.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application control.Main_Controller
Layout.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="386.0" prefWidth="354.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="Controller.java">
<children>
<MenuBar AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TextArea layoutX="14.0" layoutY="41.0" prefHeight="200.0" prefWidth="200.0" promptText="hey" />
</children>
</AnchorPane>
主类:
package control;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
public class Main_Controller extends Application{
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// TODO Auto-generated method stub
stage.setScene(new Scene(FXMLLoader.load(new File("Layout.fxml").toURI().toURL())));
stage.show();
}
}
控制类:
package control;
public class Controller {
Controller (){
System.out.println("Try");
}
}
3个类在同一个路径中。
也许对你来说是一个愚蠢的问题,但我试着用6个小时来解决我的问题-.-
答案 0 :(得分:0)
错误信息非常明显:
Caused by: java.io.FileNotFoundException: /Users/claudio/Documents/workspace/KlickiBunti/Layout.fxml (No such file or directory).
您应该将FXML文件作为资源加载:如果(当)将应用程序捆绑为jar文件,FXML将不会是文件系统上的独立文件(即使您没有,{ {1}}相对于您无法控制的工作目录进行解析。
如果FXML文件与加载它的类在同一个包中,你应该这样做
new File()
如果它与控制器类stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("Layout.fxml"))));
位于同一个包中,您也可以
Controller
此外,stage.setScene(new Scene(FXMLLoader.load(Controller.class.getResource("Layout.fxml"))));
属性期望控制器的完全限定类名(即fx:controller
),而不是Java源文件(在运行时甚至不可用) ,原因很明显。)