JavaFx在FXML中包含自定义组件

时间:2017-06-08 14:31:11

标签: java inheritance javafx fxml

我试图找到一个关于如何将自定义javafx对象包含到fxml文件的解决方案。

例如

package myExtendedObjects;

import javafx.scene.control.Label;

public class MyLabel extends Label interface connected{

    MyLabel(){
        super();
    }
  //Custom Code ...
}

进入fxml

<?import myExtendedObjects.myLabel?>
<myLabel text="Name" />

我总是从javafx.fxml.LoadException类型中获取错误代码: 也许有更好的解决方案然后创建自定义类。但我需要一个带有自定义接口的标签(已连接)。也许另一个解决方案是creata fxml文件,它只包含一个标签,并为此设置一个控制器类。

编辑:

try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/Viewer.fxml"));
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root);
        controller = fxmlLoader.getController();
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }

编辑2: 更改名称后,我能够无错误地导入自定义对象 但是当我尝试插入

<MyLabel fx:id="myLabel"/>

在我的xml日期中我收到此错误

javafx.fxml.LoadException: 
/C:/Users/TheOLGPC/Desktop/java/SolarimpactTelemety2/bin/fxml/Viewer.fxml:48

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1013)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.Main.start(Main.java:24)
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)
Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.ReflectUtil can not access a member of class myExtendedObjects.MyLabel with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1009)
... 15 more

应用程序启动方法中的异常

1 个答案:

答案 0 :(得分:1)

Java有各种naming conventions,包括类名应该大写,包名应该全部小写,字段和方法名称应该小写。

虽然Java编译器和运行时仅将这些视为约定,并且将确定任何实体是否是来自上下文的类或属性,基本上与名称无关,但在FXML和FXMLLoader中都不是这样。

FXML documentation州:

  

如果标签以大写字母

开头,则元素的标签被视为实例声明

later

  

标记名称以小写字母开头的元素表示对象属性。

因此,如果您的类无法遵循通常的命名约定,那么它们可能无法在FXML中正常工作。确保类和接口名称大写,属性名称不大写,包名称都是小写,并确保它们在Java代码和FXML中一致使用。

此外,FXMLLoader将通过调用(通常)无参数构造函数来创建类的实例。为了使其工作,构造函数必须是公共的:

package myExtendedObjects;

import javafx.scene.control.Label;

public class MyLabel extends Label interface connected{

    public MyLabel(){
        super();
    }
  //Custom Code ...
}
相关问题