java将嵌套对象从fxml映射到对象
我创建了以下对象,它应该负责在我的win应用程序中显示steeringwheel
@DefaultProperty("children")
public class SteeringWheel extends Region {
@FXML
private Circle backgroundCircle;
@FXML
private Circle innerCircle;
@FXML
private Label mylabel;
public ObservableList<Node> getChildren() {
return super.getChildren();
}
public void setCirclesLocations() {
double centerPointX = getWidth() / 2;
double centerPointY = getHeight() / 2;
setCircleLocation(backgroundCircle, centerPointX, centerPointY);
setCircleLocation(innerCircle, centerPointX, centerPointY);
}
private void setCircleLocation(Circle c, double x, double y) {
c.setCenterX(x);
c.setCenterY(y);
}
我的fxml文件包含以下内容:
<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="view.WindowController">
<center>
<VBox prefHeight="200" prefWidth="200" BorderPane.alignment="CENTER">
<children>
<SteeringWheel fx:id="steeringWheel">
<children>
<Label fx:id="mylabel" prefHeight="30.0" prefWidth="102.0" text="aileron" />
<Circle fx:id="innerCircle" fill="black" radius="40" />
<Circle fx:id="backgroundCircle" fill="darkgray" radius="100" />
</children>
</SteeringWheel>
</children>
</VBox>
</center>
.....
我试图将xml初始化映射到此对象,但它不起作用。在我的主要工作中,我试图运行setCircleLocations,但我遇到了nullpointer异常
FXMLLoader fxl=new FXMLLoader();
try {
BorderPane root = fxl.load(getClass().getResource("Window.fxml").openStream());
WindowController wc=fxl.getController(); // View
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
wc.steeringWheel.setCirclesLocations();
} catch (IOException e) {
e.printStackTrace();
}
我的窗口控制器:
public class WindowController {
@FXML
SteeringWheel steeringWheel;
}
此外,在xml文件中,我得到了SteeringWheel的子级的以下错误:Unresolved fx:id reference
堆栈跟踪:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
at view.WheelingSteer.setCircleLocation(WheelingSteer.java:36)
at view.WheelingSteer.setCirclesLocations(WheelingSteer.java:31)
at view.Main.start(Main.java:33)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Exception running application view.Main
# 1 楼答案
只有控制器注入了适当的字段。你的
SteeringWheel
类不是控制器,因此那些@FXML
注释的字段不会被注入。但是,您打算注入这些字段的对象被放入children
列表中,因此您可以在那里访问它们。然而,似乎您希望SteeringWheel
成为它自己的组件,其中两个Circle
和Label
始终存在,无论SteeringWheel
如何使用,因此尝试在单独的位置定义子级是没有意义的既然您正在扩展^ {< CD8>},您可能需要考虑使用^ a1}:
注意:我扩展了
Pane
而不是Region
。前者已经扩展了后者,并且已经进行了您所做的更改(即,将#getChildren()
方法公开,并添加@DefaultProperty("children")
注释)注意:我覆盖了
#layoutChildren()
,这样无论父对象最终的维度是什么,您的圆都保持在中心。然而,简单地将它们包装在其他一些布局中可能更容易,例如StackPane
然后在另一个FXML文件中使用
SteeringWheel
: