簡體   English   中英

錨定窗格中的錨定元素

[英]Anchoring elements in Anchor Pane

我只是使用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中的fx:controller屬性創建的fx:controller調用load 之后可用。 為此,您需要在getController 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);

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM