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

时间:2017-05-16 10:30:52

标签: java javafx fxml

我知道很多次都会问这个问题,但我找不到适用于我的解决方案(实际上我甚至无法看到我做错了什么)。

基本思想是在需要时加载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>

application的子包(称为a)中,您会发现:

ControllerA.java:

package application.a;

import java.net.URL;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;

public class ControllerA
{
    private Parent content;

    @FXML
    private Button buttonA;

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

    public Parent getContent()
    {
        return content;
    }

    @FXML
    public void clickedA(ActionEvent e)
    {
        buttonA.setText("Clicked already");
    }
}

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

我试图摆弄像

这样的路径字符串
  • &#34; ./一个/ A.fxml&#34;
  • &#34; /application/a/A.fxml"
  • &#34; A.fxml&#34;
  • &#34; / A.fxml&#34;
  • ...

但没有任何效果。如果有人可以解决这个问题,我会很放心。

2 个答案:

答案 0 :(得分:0)

我将向您展示一个最小的工作示例项目,我建议您根据它调整结构。

文件夹结构如下

enter image description here

这是主要课程     package 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>

假设我们有内容A和B,我们需要创建一个控制器和FXML每个人

package dynamic.content.javafx.content;

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

import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class ContentAController implements Initializable{

    @FXML
    private Label labl;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        labl.setText("Content A");
    }

}

现在相应的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>

以下是第二个控制器

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

}

和第二个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>

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

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

答案 1 :(得分:0)

您正在使用两种不同的方式使用FXML和控制器。如果您的FXML文件有fx:controller="SomeClass",那么FXMLLoader将实例化该类并将该实例用作控制器:换句话说,它使FXML创建控制器。

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

异常的原因是当FXMLLoader遇到fx:controller属性时,它会调用指定类的无参数构造函数:即在这种情况下它试图致电new ControllerA()。由于没有这样的构造函数,您将获得异常:

java.lang.NoSuchMethodException: application.a.ControllerA.<init>()

对于控制器构造函数的BorderPane参数的用途是什么并不是很清楚,因为你从不使用它。但是,即使你在这里有一个合适的构造函数,也会引起另一个问题:加载FXML会调用控制器的构造函数,它会加载FXML,它将调用控制器的构造函数等:你会得到一个StackOverflowException。如果要从控制器的构造函数加载FXML:

  1. 从FXML文件中删除fx:controller属性
  2. 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();
        }
    }