Java 定义控制器时,加载多个FXML文件失败

Java 定义控制器时,加载多个FXML文件失败,java,javafx,fxml,Java,Javafx,Fxml,我知道这个问题被问了很多次,但我找不到一个适合我的解决方案(实际上我甚至看不出我做错了什么) 基本思想是在需要时加载GUI组件。因此,我在各种FXML文件中构建了GUI,并实现了控制器类。FXML文件和类都存储在同一个包中,但每个组件都有一个包。只要我没有在FXML文件(fx:controller)中定义控制器类,每个FXML文件都会加载并添加到GUI中。如果它被定义,我将得到一个LoadException 为了更好地理解,这里是我的代码(简化): Main.java: package appl

我知道这个问题被问了很多次,但我找不到一个适合我的解决方案(实际上我甚至看不出我做错了什么)

基本思想是在需要时加载GUI组件。因此,我在各种FXML文件中构建了GUI,并实现了控制器类。FXML文件和类都存储在同一个包中,但每个组件都有一个包。只要我没有在FXML文件(fx:controller)中定义控制器类,每个FXML文件都会加载并添加到GUI中。如果它被定义,我将得到一个LoadException

为了更好地理解,这里是我的代码(简化):

Main.java:

package application;

import application.a.ControllerA;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application
{
    // Button aus MainLayout.fxml
    @FXML
    private Button button;

    @Override
    public void start(Stage primaryStage)
    {
        try
        {
            BorderPane root = new BorderPane();

            Parent contentMain = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));
            ControllerA contentA = new ControllerA(root);

            root.setTop(contentA.getContent());
            root.setCenter(contentMain);

            Scene scene = new Scene(root, 400, 400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

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

    // Event-Handler für den Button -> funktioniert!
    @FXML
    public void buttonClicked(ActionEvent e)
    {
        if (!button.getText().equals("NEW"))
        {
            button.setText("NEW");
        }
        else
        {
            button.setText("OLD");
        }
    }
}
此类也是以下布局的控制器(到目前为止它仍然有效):

MainLayout.fxml:

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>

<Pane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
   <children>
      <Button fx:id="button" mnemonicParsing="false" onAction="#buttonClicked" text="Button" />
   </children>
</Pane>
A.fxml:

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>

<Pane 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="application.a.ControllerA">
   <children>
      <Button fx:id="buttonA" layoutX="274.0" layoutY="188.0" mnemonicParsing="false" onAction="#clickedA" text="A" />
   </children>
</Pane>

这就是问题所在:

javafx.fxml.LoadException: 
/Z:/BachelorArbeit/Projektdateien/Entwicklung/EclipseWorkspace/Sandbox/bin/application/a/A.fxml:8

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.a.ControllerA.<init>(ControllerA.java:26)
    at application.Main.start(Main.java:35)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException: application.a.ControllerA
    at java.lang.Class.newInstance(Class.java:427)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    ... 23 more
Caused by: java.lang.NoSuchMethodException: application.a.ControllerA.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 25 more
javafx.fxml.LoadException:
/Z:/bachelorabeit/projektdateen/Entwicklung/EclipseWorkspace/Sandbox/bin/application/a/a.fxml:8
位于javafx.fxml.fxmloader.constructLoadException(fxmloader.java:2601)
在javafx.fxml.fxmloader.access$700(fxmloader.java:103)
在javafx.fxml.fxmloader$ValueElement.processAttribute(fxmloader.java:932)中
在javafx.fxml.fxmloader$InstanceDecrationElement.processAttribute中(FXMLLoader.java:971)
在javafx.fxml.fxmloader$Element.processStartElement(fxmloader.java:220)
在javafx.fxml.fxmloader$ValueElement.processStartElement(fxmloader.java:744)
在javafx.fxml.fxmloader.processStartElement(fxmloader.java:2707)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:2527)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:2441)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:3214)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:3175)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:3148)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:3124)
在javafx.fxml.fxmloader.loadImpl(fxmloader.java:3104)
在javafx.fxml.fxmloader.load(fxmloader.java:3097)
位于application.a.ControllerA.(ControllerA.java:26)
at application.Main.start(Main.java:35)
在com.sun.javafx.application.launchempl.lambda$launchApplication1$162(launchempl.java:863)
位于com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
位于com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
位于java.security.AccessController.doPrivileged(本机方法)
位于com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95)
在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法)
位于com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
运行(Thread.java:745)
原因:java.lang.InstanceException:application.a.ControllerA
位于java.lang.Class.newInstance(Class.java:427)
位于sun.reflect.misc.reflectil.newInstance(reflectil.java:51)
在javafx.fxml.fxmloader$ValueElement.processAttribute(fxmloader.java:927)中
... 23多
原因:java.lang.NoSuchMethodException:application.a.ControllerA。()
位于java.lang.Class.getConstructor0(Class.java:3082)
位于java.lang.Class.newInstance(Class.java:412)
... 25多
我试着像这样摆弄路径字符串

  • “/a/a.fxml”
  • “/application/a/a.fxml”
  • “A.fxml”
  • “a/a.fxml”

但什么都没用。如果有人能阻止我解决这个问题,我会非常放心。

我将向您展示一个最小的工作示例项目,并建议您调整您的结构以适应它

文件夹结构如下所示

这是主课 包dynamic.content.javafx

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public final class Main extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        Scene scene = new Scene(root, 300, 200);

        stage.setTitle("FXML Welcome");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
主控制器

package dynamic.content.javafx;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;

public final class MainController implements Initializable{

    @FXML
    private Button btnSwitch;

    @FXML
    private AnchorPane contentPane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
    }

    private boolean swtch = false;
    @FXML
    public void handleContentSwitch() throws IOException{
        Parent contentNode = null;

        if(swtch){
            System.out.println("loading content A");
            contentNode = FXMLLoader.load(getClass().getResource("./content/content_a.fxml"));          
        }else{
            System.out.println("loading content B");
            contentNode = FXMLLoader.load(getClass().getResource("./content/content_b.fxml"));
        }
        contentPane.getChildren().clear();
        contentPane.getChildren().add(contentNode);

        swtch = !swtch;
    }

}
主FXML

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dynamic.content.javafx.MainController">
   <children>
      <Button fx:id="btnSwitch" onAction="#handleContentSwitch" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" text="Switch Content" />
      <AnchorPane fx:id="contentPane" prefHeight="150.0" prefWidth="300.0" />
   </children>
</VBox>
现在是相应的FXML

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dynamic.content.javafx.content.ContentAController">
   <children>
      <Label fx:id="labl" alignment="CENTER" contentDisplay="CENTER" text="Label" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</AnchorPane>
第二个是FXML

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dynamic.content.javafx.content.ContentBController">
   <children>
      <Label fx:id="labl" alignment="CENTER" contentDisplay="CENTER" text="Label" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</AnchorPane>

如果您知道按下按钮,内容将动态加载


如果您有任何问题,请告诉我:-)

您正在混合使用FXML和控制器的两种不同方法。如果您的FXML文件具有
fx:controller=“SomeClass”
,则
fxmloader
将实例化该类并将实例用作控制器:换句话说,它使FXML创建控制器

另一方面,控制器的构造函数加载FXML,因此控制器也可以创建FXML中定义的UI

出现异常的原因是当
fxmloader
遇到
fx:controller
属性时,它调用指定类的无参数构造函数:即,在这种情况下,它尝试调用
new ControllerA()
。由于没有此类构造函数,因此会出现异常:

java.lang.NoSuchMethodException:application.a.ControllerA.()
对于控制器的构造函数,
BorderPane
参数的用途并不十分清楚,因为您从未使用过它。然而,即使您在这里有一个合适的构造函数,它也会导致另一个问题:加载FXML会调用控制器的构造函数,加载FXML会调用控制器的构造函数,等等:您会得到一个
StackOverflowException
。如果要从控制器的构造函数加载FXML:

  • 从FXML文件中删除
    fx:controller
    属性
  • FXMLLoader
    上的控制器显式设置为当前控制器实例:

    public ControllerA(BorderPane root)
    {
        String sceneFile = "A.fxml";
        URL url = null;
        try
        {
            url = getClass().getResource(sceneFile);
            FXMLLoader loader = new FXMLLoader(url);
            loader.setController(this);
            content = loader.load();
        }
        catch (Exception ex)
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }
    

  • 这是
    Main.java
    ?Ups中的第35行,很抱歉:
    ControllerA contentA=newcontrollera(root)
    ControllerA.java
    content=fxmloader.load(url)中的第26行给你的项目结构多一点。似乎Java找不到fxml文件。还有,看看这个m
    
    package dynamic.content.javafx.content;
    
    import java.net.URL;
    import java.util.ResourceBundle;
    
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Label;
    
    public class ContentBController implements Initializable{
    
        @FXML
        private Label labl;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            labl.setText("Content B");
        }
    
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    
    
    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dynamic.content.javafx.content.ContentBController">
       <children>
          <Label fx:id="labl" alignment="CENTER" contentDisplay="CENTER" text="Label" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
       </children>
    </AnchorPane>
    
    public ControllerA(BorderPane root)
    {
        String sceneFile = "A.fxml";
        URL url = null;
        try
        {
            url = getClass().getResource(sceneFile);
            FXMLLoader loader = new FXMLLoader(url);
            loader.setController(this);
            content = loader.load();
        }
        catch (Exception ex)
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }