【问题标题】:Difficulty loading FXML file in Eclipse在 Eclipse 中难以加载 FXML 文件
【发布时间】:2017-08-05 00:45:54
【问题描述】:

这是我的课程,我在其中编写了一个内嵌代码以将 fxml 文件链接到我正在处理的项目:

package application;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;


public class towerOfHanoi extends Application implements Initializable 
{
    public static void main(String[] args) 
    {
        launch(args);
    }
    public void start(Stage primaryStage) throws Exception 
    {       
        try
        {
            Parent rootContainer = FXMLLoader.load(getClass().getResource("/application/userInterface.fxml"));
        Scene s=new Scene(rootContainer);
        primaryStage.setScene(s);
        //primaryStage.setTitle("Towers Of Hanoi");
        primaryStage.show();
        }
        catch(IOException e)
        {               
        //          e.printStackTrace();
        }
}

@Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }
}

每当我尝试在 Eclipse 中运行它时,什么都没有发生。要终止的方块是红色的,好像发生了什么事,但我在 scenebuilder 中创建的 UI 没有显示。

****更新: 这是我取消注释那段代码时的完整堆栈跟踪:

Mar 14, 2017 4:01:49 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.102 by JavaFX runtime of version 8.0.101
javafx.fxml.LoadException: No controller specified.
/F:/2nd%20Year%20College%20Stuff/Semester%202/Event-driven%20Programming/2nd%20Year%20Workspace/CA%202/bin/application/userInterface.fxml:44

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    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 application.towerOfHanoi.start(towerOfHanoi.java:29)
    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)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)

【问题讨论】:

  • 尝试取消注释 e.printStackTrace();
  • 另外,请注意 Initializable 仅对控制器类调用...您通常不会在 Application 子类中使用它...
  • 如果我把它注释掉,我会得到一个非常大的错误。
  • 我已经用堆栈跟踪更新了 OP。
  • 不要破坏你的帖子。

标签: java eclipse javafx fxml scenebuilder


【解决方案1】:

正如错误告诉您的那样:您需要将控制器 关联 到 .fxml,如下所示:

FILE userInterface.fxml(您可以拥有除 BorderPane 之外的另一个容器,但它是相同的,它必须在根父级上)

<BorderPane fx:id="background" fx:controller="application.Controller">
  //Content ...
</BorderPane>

您可以将控制器设置为 fxml(见上文)或通过 SceneBuilder:


另外,将 Launcher 和控制器分开会更清晰,如下所示:

文件 towerOfHanoi.java

public class towerOfHanoi extends Application{
    public static void main(String[] args){
        launch(args);
    }
    public void start(Stage primaryStage) throws Exception{       
        try{
            Parent rootContainer = FXMLLoader.load(getClass().getResource("/application/userInterface.fxml"));
            Scene s=new Scene(rootContainer);
            primaryStage.setScene(s);
            //primaryStage.setTitle("Towers Of Hanoi");
            primaryStage.show();
        }
        catch(IOException e){               
        //          e.printStackTrace();
        }
    }
}

FILE Con​​troller.java

public class Controller implements Initializable {
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }
}

【讨论】:

  • 那您需要我添加到 OP 中以帮助查找原因或错误?
  • 好的,所以现在 OP 包含堆栈跟踪,这确实回答了这个问题。 :)。
  • @James_D 在我回答后他更新了堆栈跟踪,我已经更新了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 2021-04-29
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多