设置root的JavaFx FXML加载文件问题 [英] JavaFx FXML load file issues with setting root

查看:1139
本文介绍了设置root的JavaFx FXML加载文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javaFx的新手,并且想要使用场景构建器进行GUI开发,我遇到了一个问题,没有运气搜索网站或一般网络我的问题,虽然已经提出了类似的问题,认为不同的观点可能是需要。我试图在快速构建之后通过Netbeans加载FXML文件以测试功能,因此代码很简单,但我无法在控制器中设置根文件。我的代码是以下
公共类Divergex扩展Application {

New to javaFx and wanting to use scenebuilder for GUI development, i've come across an issue with no luck searching the website nor the web in general for my problem, although similar questions have been asked, thought a different perspective could be needed. I am trying to load an FXML file through Netbeans after a quick build to test functionality so the code is simple, but i cannot get the root file to be set in the controller. my code is the following public class Divergex extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("DivergexGUI.fxml"));


    Scene scene = new Scene(root);
    scene.setRoot(root);

    stage.setScene(scene);
    stage.show();
}

我已经尝试过将fxroot更改为Vbox而没有运气的建议,我继续在编译时得到Load异常:

Ive tried suggestions in changing fxroot to a Vbox with no luck, i continue to get a Load exception on the compile :


Application start方法中的异常...
引起:javafx.fxml.LoadException :尚未设置Root。在加载之前使用方法setRoot()。

Exception in Application start method... Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.

当我使用

scene.setRoot(root); 

遇到同样的例外

i我已经将问题缩小到这样一个事实,即我的FXML文件无法在Parent对象中设置为root,但却无法解决这个问题。任何建议都会非常感谢。

i've narrowed the issue down to the fact that my FXML file is unable to be set as a root in the Parent object but have had no luck in tackling this. Any suggestions would be great thanks.

推荐答案

< fx:root> 为FXML文件指定动态根;这意味着FXML文件的根是在加载文件之前在加载器上设置的对象。这通常用于自定义控件,您希望控件是 Node 的子类,可以使用常规Java代码进行实例化,但希望使用FXML定义其布局。 < fx:root> (或至少一个如何使用它的示例)。 oracle.com/javase/8/javafx/api/toc.htmrel =noreferrer>标准文档。特别是,如果您使用< fx:root> ,您必须:

<fx:root> specifies a "dynamic root" for your FXML file; this means the root of the FXML file is an object that is set on the loader prior to loading the file. This is typically used for custom controls, where you want the control to be a subclass of Node that can be instantiated using regular Java code, but want to define its layout using FXML. Proper use of <fx:root> (or at least an example of how it can be used) is shown in the standard documentation. In particular, if you use <fx:root> you must:


  1. 创建一个 FXMLLoader 实例,而不是使用静态便利 FXMLLoader.load(URL)方法

  2. 在该实例上调用setRoot(...),并传入作为FXML根的对象。

  1. Create an FXMLLoader instance, instead of using the static convenience FXMLLoader.load(URL) method
  2. Call setRoot(...) on that instance, and pass in the object that is to be the root of the FXML.

对于标准FXML使用,您只需使用常规实例声明作为根。几乎所有可用的示例都是这样的:可能最好的起点是官方教程。在你的情况下,因为你想要一个 VBox ,你可能只需要

For standard FXML use, you just use a regular instance declaration as the root. Almost every example available works this way: probably the best place to start is the official tutorial. In your case, since you want a VBox, you probably just need

<VBox xmlns="javafx.com/javafx/8"; xmlns:fx="javafx.com/fxml/1"; fx:controller="divergex.DivergexGUIController">
<!-- ... -->
</VBox>

编辑如果Netbeans给你带来问题,我建议你使用Eclipse e(fx)clipse插件。有一个非常准确的,但几乎所有你需要的,教程

Edit If Netbeans is giving you issues, I recommend using Eclipse with the e(fx)clipse plugin. There's a very barebones, but pretty much all you need, tutorial.

这篇关于设置root的JavaFx FXML加载文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆