简体   繁体   English

应用程序启动方法中的异常-Javafx控件类

[英]Exception in Application start method - Javafx control class

I have already read all the old question, but I don´t understand what´s happened. 我已经读完所有旧问题,但是我不明白发生了什么。 The Problem come only when I try to connect the file layout.fxml with the control Class. 仅当我尝试将文件layout.fxml与控件类连接时,问题才会出现。 I have try 1.000 path but it doesn´t work... I need your help, and sorry for the stupid question :( 我尝试了1.000路径,但是它不起作用...我需要您的帮助,对于这个愚蠢的问题:(

error: 错误:

> 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 Class: 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>

Main Class: 主类:

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();

    }

}

Control Class: 控制类别:

package control;

public class Controller {

    Controller (){

        System.out.println("Try");

    }


}

The 3 Classes are in the same path. 3个类位于同一路径中。

Maybe is for you a stupid question, but I try since 6 hours to resolve my Problem -.- 也许对您来说是一个愚蠢的问题,但是我从6个小时以来一直尝试解决我的问题-.-

The error message is pretty self-explanatory: 错误消息是很不言自明的:

Caused by: java.io.FileNotFoundException: /Users/claudio/Documents/workspace/KlickiBunti/Layout.fxml (No such file or directory).

You should load the FXML file as a resource anyway: if (when) you bundle the application as a jar file, the FXML will not be an independent file on the file system anyway (and even if you don't, new File() resolves relative to the working directory, over which you have no control). 无论如何,您都应该将FXML文件作为资源加载:如果将应用程序捆绑为jar文件,则FXML无论如何都不是文件系统上的独立文件(即使您不这样做,也可以使用new File()相对于您无法控制的工作目录进行解析)。

If the FXML file is in the same package as the class from which it is being loaded, you should do 如果FXML文件与从中加载该文件的类位于同一包中,则应执行

stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("Layout.fxml"))));

If it is in the same package as the controller class Controller , you could also do 如果它与控制器类Controller在同一包中,则也可以执行

stage.setScene(new Scene(FXMLLoader.load(Controller.class.getResource("Layout.fxml"))));

Additionally, the fx:controller attribute is expecting the fully qualified classname of the controller (ie package.ClassName ), not the Java source file (which is not even available at runtime, for obvious reasons). 另外, fx:controller属性期望fx:controller完全合格的类名 (即package.ClassName ),而不是Java源文件(出于明显的原因,它甚至在运行时都不可用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM