我试图找到一个关于如何将自定义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 有各种命名约定,包括类名应大写,包名应全部小写,字段和方法名应以小写开头。
虽然 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 ...
}