使用 FXML 切换场景时出错

Error switching Scenes with FXML

所以我想在两个 FXML 文件之间切换场景,但是如果我尝试启动应用程序,我会收到此错误消息:

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$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#handleButtonAction', either the event handler is not in the Namespace or there is an error in the script.

 /C:/Users/j3Ypi/Dropbox/05_Praktikum/survey/out/production/survey/survey/view/moduleA.fxml:28
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at survey.Main.showModuleA(Main.java:40)
at survey.Main.start(Main.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
... 1 more
Exception running application survey.Main 

我尝试了几种方法来加载 FXML 文件,但每次都出现相同的错误。我还尝试了此处社区建议的多种潜在解决方案,但 none 似乎解决了我的问题。他们中的大多数人说这是正确路径的问题,但我在控制器中加载了与主应用程序中完全相同的路径,并且在控制器中没有 handleButtonAction() 应用程序正常启动。我现在在这个基本任务上坚持了几个小时。我使用 IDEA,这是我的代码:

主要:

package survey;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

private Stage primaryStage;
private BorderPane rootLayout;

@Override
public void start(Stage primaryStage) throws Exception{
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Modul A");

    initRootlayout();

    showModuleA();
}

private void  initRootlayout() throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/rootLayout.fxml"));
        rootLayout = loader.load();

        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
}

private void showModuleA() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/moduleA.fxml"));
        AnchorPane rootPane = loader.load();

        rootLayout.setCenter(rootPane);
}


public static void main(String[] args) {
    launch(args);
}
}

控制器:

package survey.view;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class moduleAController implements Initializable {

@FXML private Button bBack;
@FXML private Button bForward;

@FXML
private void handleButtonAction(ActionEvent event) throws IOException{

    Stage stage;
    Parent root;

    if (event.getSource() == bBack){
        stage = (Stage) bBack.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("view/moduleB.fxml"));
    } else {
        stage = (Stage) bForward.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("view/moduleA.fxml"));
    }

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

}

@Override
public void initialize(URL location, ResourceBundle resources) {

}
}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<AnchorPane fx:id="rootPane" prefHeight="470.0" prefWidth="950.0" styleClass="background" stylesheets="@DarkTheme.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="survey.view.moduleAController">
   <children>
      <BorderPane layoutX="12.0" prefHeight="470.0" prefWidth="950.0">
         <top>
            <GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="62.0" prefWidth="450.0" BorderPane.alignment="BOTTOM_RIGHT">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
               <children>
                  <Button fx:id="bBack" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="30.0" prefWidth="110.0" text="Zurück" GridPane.columnIndex="1" />
                  <Button fx:id="bForward" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="30.0" prefWidth="110.0" text="Vorwärts" GridPane.columnIndex="2" />
                  <Label fx:id="lModul" text="Modul:" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
           </children>
        </GridPane>
     </top>
         <left>
            <GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="100.0" BorderPane.alignment="CENTER">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
              <rowConstraints>
                  <RowConstraints maxHeight="126.0" minHeight="10.0" prefHeight="95.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="126.0" minHeight="10.0" prefHeight="95.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="180.0" minHeight="10.0" prefHeight="104.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="253.0" minHeight="10.0" prefHeight="95.0" vgrow="SOMETIMES" />
          </rowConstraints>
               <children>
                  <Label fx:id="lFrageA" text="1. Frage A" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
                  <Label fx:id="lFrageB" text="2. Frage B" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
                  <Label fx:id="lFrageC" text="3. Frage C" GridPane.halignment="CENTER" GridPane.rowIndex="3" />
              <Label text="Modul A" GridPane.halignment="CENTER" />
           </children>
        </GridPane>
     </left>
         <center>
            <GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="189.0" BorderPane.alignment="CENTER">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
              <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
               <children>
                  <RadioButton fx:id="bCheckA1" mnemonicParsing="false" GridPane.halignment="CENTER" GridPane.rowIndex="1">
                     <toggleGroup>
                        <ToggleGroup fx:id="groupA" />
                 </toggleGroup>
              </RadioButton>
                  <RadioButton fx:id="bCheckA2" mnemonicParsing="false" toggleGroup="$groupA" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
                  <RadioButton fx:id="bCheckB1" mnemonicParsing="false" GridPane.halignment="CENTER" GridPane.rowIndex="2">
                     <toggleGroup>
                        <ToggleGroup fx:id="groupB" />
                 </toggleGroup></RadioButton>
                  <RadioButton fx:id="bCheckB2" mnemonicParsing="false" toggleGroup="$groupB" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
                  <RadioButton fx:id="bCheckC1" mnemonicParsing="false" GridPane.halignment="CENTER" GridPane.rowIndex="3">
                     <toggleGroup>
                        <ToggleGroup fx:id="groupC" />
                 </toggleGroup></RadioButton>
                  <RadioButton fx:id="bCheckC2" mnemonicParsing="false" toggleGroup="$groupC" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="3" />
                  <Label fx:id="lTrue" text="trifft zu" GridPane.halignment="CENTER" />
                  <Label fx:id="lFalse" text="trifft nicht zu" GridPane.columnIndex="1" GridPane.halignment="CENTER" />
           </children>
        </GridPane>
     </center>
         <bottom>
            <GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="62.0" prefWidth="600.0" BorderPane.alignment="BOTTOM_CENTER">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
               <children>
                  <Button mnemonicParsing="false" prefHeight="30.0" prefWidth="160.0" text="Neue Befragung" GridPane.halignment="CENTER" />
                  <Button fx:id="bSave" mnemonicParsing="false" prefHeight="30.0" prefWidth="130.0" text="Speichern" GridPane.columnIndex="1" GridPane.halignment="CENTER" />
                  <Button mnemonicParsing="false" prefHeight="30.0" prefWidth="130.0" text="Auswertung" GridPane.columnIndex="2" GridPane.halignment="CENTER" />
           </children>
        </GridPane>
     </bottom>
         <right>
            <GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="300.0" BorderPane.alignment="CENTER_RIGHT">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
              <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
               <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
               <children>
                  <TextArea maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="70.0" prefWidth="250.0" GridPane.rowIndex="1" />
                  <TextArea maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="70.0" prefWidth="250.0" GridPane.rowIndex="2" />
                  <TextArea maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="70.0" prefWidth="250.0" GridPane.rowIndex="3" />
                  <Label text="Kommentar" />
           </children>
        </GridPane>
     </right>
  </BorderPane>
</children>
</AnchorPane>

感谢您的帮助。提前致谢!

编辑: 除了动作事件的错误导入之外,路径中也存在错误。控制器位于视图文件夹中,因此路径只是 moduleB.fxml 而不是 view/moduleB.fxml

感谢您的帮助!

第二个问题的解决方案如下:

root = FXMLLoader.load(getClass().getClassLoader().getResource("xxx/yyy/view/moduleB.fxml)"

其中 xxx 和 yyy 是您的 java 项目的包结构。例如 src/foo/var/view/moduleB.fxml -> "foo/var/view/moduleB.fxml"

为什么你试图在运行时加载它,你需要添加 getClassLoader() 否则你将从控制器获得实际的 class 路径,因为你正在调用 getClass() 控制器中的方法。这就是它找不到您的 fxml 文件的原因。它希望它位于控制器包下的视图包中。我希望它现在有效:)

第一个问题:

您的控制器脚本中出现了一点错误。您导入了错误的 ActionEvent。您导入了

import java.awt.event.ActionEvent;

但需要导入

import javafx.event.ActionEvent;

这应该可以解决问题。