我刚刚使用Scene Builder制作了这三个文件:
textfield.fxml:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="mailbox.TextFieldController" xmlns:fx="http://javafx.com/fxml/1">
<TextField fx:id="id" layoutX="252.0" layoutY="39.0" prefHeight="27.0" prefWidth="35.0" />
<TextField fx:id="mitt" layoutX="252.0" layoutY="73.0" />
<TextField fx:id="dest" layoutX="252.0" layoutY="108.0" />
<TextField fx:id="oggetto" layoutX="252.0" layoutY="144.0" />
<TextField fx:id="data" layoutX="308.0" layoutY="39.0" prefHeight="27.0" prefWidth="110.0" />
</AnchorPane>
textarea.fxml:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="mailbox.TextAreaController" xmlns:fx="http://javafx.com/fxml/1">
<TextArea id="textarea" editable="false" layoutX="240.0" layoutY="200.0" prefHeight="200.0" prefWidth="360.0" />
</AnchorPane>
lista.fxml:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="mailbox.ListController" xmlns:fx="http://javafx.com/fxml/1">
<ListView id="listView" layoutY="29.0" prefHeight="371.0" prefWidth="240.0" />
</AnchorPane>
现在,我通过将元素传递到Anchor Pane的构造函数中并使用AddAll方法添加了元素,但是它不起作用:
public void start(Stage stage) throws Exception {
FXMLLoader listLoader = new FXMLLoader(getClass().getResource("lista.fxml"));
ListController listController = listLoader.getController();
FXMLLoader textareaLoader = new FXMLLoader(getClass().getResource("textarea.fxml"));
TextAreaController textareaController = textareaLoader.getController();
FXMLLoader fieldLoader = new FXMLLoader(getClass().getResource("textfield.fxml"));
TextFieldController fieldController = fieldLoader.getController();
AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load());
//root.getChildren().addAll(listLoader.load(), textareaLoader.load(), fieldLoader.load());
DataModel model = new DataModel();
listController.initModel(model);
textareaController.initModel(model);
Scene scene = new Scene(root, 400, 600);
stage.setScene(scene);
stage.show();
}
这是错误:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at mailbox.MailBox.start(MailBox.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application mailbox.MailBox
答案 0 :(得分:0)
基于fxml中的fx:controller
属性创建的控制器仅在 之后调用load
可用。为此,您需要在getController
创建后进行AnchorPane
调用:
FXMLLoader listLoader = new FXMLLoader(getClass().getResource("lista.fxml"));
FXMLLoader textareaLoader = new FXMLLoader(getClass().getResource("textarea.fxml"));
FXMLLoader fieldLoader = new FXMLLoader(getClass().getResource("textfield.fxml"));
AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load());
ListController listController = listLoader.getController();
TextAreaController textareaController = textareaLoader.getController();
TextFieldController fieldController = fieldLoader.getController();
DataModel model = new DataModel();
listController.initModel(model);
textareaController.initModel(model);
...