Создать два отдельных этапа в JavaFX - PullRequest
0 голосов
/ 21 ноября 2018

Я создал приложение, которое должно открывать окно при нажатии определенной кнопки.Теперь первая часть (где я создаю макет со всеми кнопками / текстовыми полями / и т. Д.) Работает, но когда я пытаюсь установить действие, которое должна выполнять кнопка, я получаю список ошибок на самой кнопке.Это код моего контроллера кнопок:

public class ButtonController {

@FXML
private Button scrivi;

@FXML
private Button reply;

@FXML
private Button replyall;

@FXML
private Button forward;

private DataModel model;

public void initModel(DataModel model) {
    if (this.model != null) {
        throw new IllegalStateException("Model can only be initialized once");
    }
    this.model = model;

    scrivi.setOnMouseClicked((event) -> {
        try {
            FXMLLoader pagina = new FXMLLoader(getClass().getResource("panel.fxml"));
            BorderPane root = new BorderPane(pagina.load());
            PanelController pc = pagina.getController();
            pc.initModel(model);
            Scene scene = new Scene(root, 603, 403);
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            Logger logger = Logger.getLogger(getClass().getName());
            logger.log(Level.SEVERE, "Failed to create new Window.", e);
        }
    });
}

Это FXML панели, которую я хочу создать:

    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="PanelController">
   <top>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <TextField fx:id="dest" layoutX="53.0" layoutY="51.0" />
            <TextField fx:id="cc" layoutX="53.0" layoutY="92.0" />
            <TextField fx:id="mitt" layoutX="53.0" layoutY="135.0" />
            <Label layoutX="29.0" layoutY="56.0" text="A:" />
            <Label layoutX="24.0" layoutY="97.0" text="CC:" />
            <Label layoutX="25.0" layoutY="140.0" text="Da:" />
            <TextField fx:id="obj" layoutX="53.0" layoutY="173.0" />
            <Label layoutX="22.0" layoutY="178.0" text="Obj:" />
            <Button layoutX="539.0" layoutY="14.0" mnemonicParsing="false" text="Invia" />
         </children>
      </AnchorPane>
   </top>
   <bottom>
      <TextArea id="testo" prefHeight="182.0" prefWidth="564.0" BorderPane.alignment="CENTER" />
   </bottom>
</BorderPane>

Это часть (которая работает) первогопанель приложения:

public class MailBox extends Application {

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader listLoader = new FXMLLoader(getClass().getResource("lista.fxml"));
    FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("menubar.fxml"));
    FXMLLoader textareaLoader = new FXMLLoader(getClass().getResource("textarea.fxml"));
    FXMLLoader fieldLoader = new FXMLLoader(getClass().getResource("textfield.fxml"));
    FXMLLoader buttonLoader = new FXMLLoader(getClass().getResource("button.fxml"));

    AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load(), menuLoader.load(), buttonLoader.load());

    ListController listController = listLoader.getController();
    MenuBarController menuController = menuLoader.getController();
    TextAreaController textareaController = textareaLoader.getController();
    TextFieldController fieldController = fieldLoader.getController();
    ButtonController buttonController = buttonLoader.getController();

    DataModel model = new DataModel();
    listController.initModel(model);
    menuController.initModel(model);
    textareaController.initModel(model);
    fieldController.initModel(model);
    buttonController.initModel(model);

    Scene scene = new Scene(root, 603, 403);
    stage.setScene(scene);
    stage.show();
}

Это ошибка, которую я получаю при попытке запустить программу:

Exception in Application start method
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.ButtonController.initModel(ButtonController.java:47)
	at mailbox.MailBox.start(MailBox.java:43)
	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
...