JavaFx 在 FXML 中包含自定义组件
JavaFx Include custom components in 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 中得到错误代码:
也许有比创建自定义 classes 更好的解决方案。但我需要一个带有自定义界面(已连接)的标签。也许另一种解决方案是创建一个仅包含标签的 fxml 文件,并为此使用界面设置一个控制器 class。
编辑:
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.access0(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$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(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 有多种 naming conventions,包括 class 名称应大写,包名称应全部小写,字段和方法名称应小写。
虽然 Java 编译器和运行时仅将这些视为约定,并将根据上下文确定任何实体是 class 还是 属性,基本上与名称无关,在 FXML 和 FXMLLoader
.
中情况并非如此
an element's tag is considered an instance declaration if the tag begins with uppercase letter
Elements whose tag names begin with a lowercase letter represent object properties.
因此,如果您的 classes 不遵循通常的命名约定,它们可能无法在 FXML 中正常工作。确保 class 和接口名称大写,属性 名称不大写,包名称全部小写,并确保它们在 Java 代码和FXML.
此外,FXMLLoader
将通过(通常)调用无参数构造函数来创建您的 class 的实例。为了让它工作,构造函数必须是 public:
package myExtendedObjects;
import javafx.scene.control.Label;
public class MyLabel extends Label interface connected{
public MyLabel(){
super();
}
//Custom Code ...
}
我试图找到有关如何将自定义 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 中得到错误代码: 也许有比创建自定义 classes 更好的解决方案。但我需要一个带有自定义界面(已连接)的标签。也许另一种解决方案是创建一个仅包含标签的 fxml 文件,并为此使用界面设置一个控制器 class。
编辑:
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.access0(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$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(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 有多种 naming conventions,包括 class 名称应大写,包名称应全部小写,字段和方法名称应小写。
虽然 Java 编译器和运行时仅将这些视为约定,并将根据上下文确定任何实体是 class 还是 属性,基本上与名称无关,在 FXML 和 FXMLLoader
.
an element's tag is considered an instance declaration if the tag begins with uppercase letter
Elements whose tag names begin with a lowercase letter represent object properties.
因此,如果您的 classes 不遵循通常的命名约定,它们可能无法在 FXML 中正常工作。确保 class 和接口名称大写,属性 名称不大写,包名称全部小写,并确保它们在 Java 代码和FXML.
此外,FXMLLoader
将通过(通常)调用无参数构造函数来创建您的 class 的实例。为了让它工作,构造函数必须是 public:
package myExtendedObjects;
import javafx.scene.control.Label;
public class MyLabel extends Label interface connected{
public MyLabel(){
super();
}
//Custom Code ...
}