JavaFx 在 FXML 中包含自定义组件

问题描述 投票:0回答:1

我尝试找到有关如何将自定义 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 类型中获取错误代码: 也许有比创建自定义类更好的解决方案。但我需要一个带有自定义接口(已连接)的标签。也许另一种解决方案是创建一个仅包含标签的 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

应用程序启动方法出现异常

java inheritance javafx fxml
1个回答
2
投票

Java 有各种命名约定,包括类名应大写、包名应全部小写、字段和方法名称应以小写开头。

虽然 Java 编译器和运行时仅将这些视为约定,并且将根据上下文确定任何实体是否是类或属性(本质上与名称无关),但在 FXML 和

FXMLLoader
中却并非如此。

FXML 文档 指出:

如果元素的标签以大写字母开头,则该标签被视为实例声明

稍后

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

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

此外,

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

package myExtendedObjects;

import javafx.scene.control.Label;

public class MyLabel extends Label interface connected{

    public MyLabel(){
        super();
    }
  //Custom Code ...
}
© www.soinside.com 2019 - 2024. All rights reserved.