【问题标题】:Anchoring elements in Anchor Pane锚定窗格中的锚定元素
【发布时间】:2019-04-15 04:23:55
【问题描述】:

我刚刚用 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

【问题讨论】:

  • 仅查看您的代码的旁注。考虑到您发布的FXML's,使用AnchorPane 作为根似乎不是最好的节点。

标签: java javafx listener anchor


【解决方案1】:

基于 fxml 中的fx:controller 属性创建的控制器仅在调用load 之后 可用。由于这个原因,您需要在 AnchorPane 创建之后执行 getController 调用:

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);

...

【讨论】:

  • 我试过了,但我得到了同样的错误。我应该发布其他课程吗?
  • 您应该确定导致 NPE 的行并找出哪个表达式产生null。遍历代码中取消引用的所有表达式,修改代码后,start 中应该没有可能导致 NPE 的表达式(at mailbox.MailBox.start(MailBox.java:38) 不应该是 NPE 堆栈中提到的最顶层代码行)。也许您的initModel 调用会导致 NPE,这将是一个不同的问题。
猜你喜欢
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多