dependency-injection - Afterburner.fx fxml 加载错误

标签 dependency-injection javafx fxml fxmlloader

我尝试在我的项目中将 Afterburner.fx 用于 DI。我以 followme.fx 为例,并尝试将其应用于我的项目。但我不知道出了什么问题,因为我遵循示例但是当我运行应用程序时,我得到了这些异常:

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:497)
    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:497)
    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$151(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1030870354.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2438)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
    at com.airhacks.afterburner.views.FXMLView.loadSynchronously(FXMLView.java:87)
    at com.airhacks.afterburner.views.FXMLView.initializeFXMLLoader(FXMLView.java:96)
    at com.airhacks.afterburner.views.FXMLView.getView(FXMLView.java:108)
    at com.******.controladores.MainApp.start(MainApp.java:44)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$158(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/1821793753.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$171(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/1637506559.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$169(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/313834886.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$170(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/2117255219.run(Unknown Source)
    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$144(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/764308918.run(Unknown Source)
    ... 1 more
Exception running application com.******.controladores.MainApp

目录结构:

Files that I have in my project

主类

package com.******.controladores;

import com.airhacks.afterburner.injection.Injector;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import static java.util.logging.Logger.getLogger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

    private static final Logger LOG = getLogger(MainApp.class.getName());

    @Override
    public void start(Stage stage) {        
        Map<Object, Object> customProperties = new HashMap<>();
        Injector.setConfigurationSource(customProperties::get);
        LoginView loginView = new LoginView();
        Scene scene = new Scene(loginView.getView());
        stage.setScene(scene);
        stage.show();
    }

    @Override
    public void stop() throws Exception {
        Injector.forgetAll();
    }

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

}

LoginView 类

package com.******.controladores;

import com.airhacks.afterburner.views.FXMLView;

public class LoginView extends FXMLView {

}

LoginPresenter 类

package com.****.controladores;

import com.****.dao.usuarioDAO;
import com.****.modelos.usuario;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import static java.util.logging.Logger.getLogger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.PasswordField;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javax.inject.Inject;

public class LoginPresenter implements Initializable {

    private static final Logger LOG = getLogger(LoginPresenter.class.getName());

    @FXML
    Button btnAceptar;
    @FXML
    ChoiceBox lstUsuario;
    @FXML
    PasswordField txtClave;

    @Inject
    private DataModel datos;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        List<Usuario> usuarios = new UsuarioDAO().obtenListausuariosPorEstado(true);

        lstUsuario.getItems().addAll(usuarios);
        btnAceptar.setOnAction((ActionEvent event) -> {
            validarCampos();
        });
        txtClave.setOnKeyPressed((KeyEvent ke) -> {
            if (ke.getCode().equals(KeyCode.ENTER)) {
                validarCampos();
            }
        });
    }
}

Login.fxml

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

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


<VBox alignment="CENTER" prefHeight="338.0" prefWidth="286.0" styleClass="fondo" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.****.controladores.LoginPresenter">
    <children>
        <Label alignment="CENTER" contentDisplay="CENTER" nodeOrientation="LEFT_TO_RIGHT" styleClass="titulo" text="Inicio de sesión">
            <font>
                <Font name="Segoe UI" size="30.0" />
            </font>
        </Label>
        <Pane prefHeight="59.0" prefWidth="286.0" />
        <VBox alignment="CENTER">
            <children>
                <Label text="Usuario">
                    <font>
                        <Font name="System Bold" size="12.0" />
                    </font>
                </Label>
                <ChoiceBox fx:id="lstUsuario" prefWidth="150.0" />
                <Label text="Clave">
                    <font>
                        <Font name="System Bold" size="12.0" />
                    </font>
                </Label>
                <PasswordField fx:id="txtClave" promptText="Introduce tu clave" />
            </children>
        </VBox>
        <Pane prefHeight="59.0" prefWidth="286.0" />
        <Button fx:id="btnAceptar" contentDisplay="CENTER" mnemonicParsing="false" text="Aceptar" textAlignment="CENTER">
        </Button>
        <Pane prefHeight="59.0" prefWidth="286.0" />
    </children>
</VBox>

POM

    <dependency>
        <groupId>com.airhacks</groupId>
        <artifactId>afterburner.fx</artifactId>
        <version>1.6.0</version>
    </dependency>

最佳答案

Caused by: java.lang.IllegalStateException: Location is not set. 

您的 JAR 中可能没有 *.fxml 文件。我有同样的问题。尝试将资源过滤添加到您的 POM 文件中,如下所示:https://github.com/AdamBien/afterburner.fx/blob/master/pom.xml

    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.fxml</include>
                <include>**/*.css</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/test/java</directory>
            <includes>
                <include>**/*.fxml</include>
                <include>**/*.css</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.css</include>
                <include>**/*.css</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>

这允许将 .fxml 文件复制到 JAR 文件中的包中。

关于dependency-injection - Afterburner.fx fxml 加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27111182/

相关文章:

java - 如何在边框底部的每个角放置两个按钮

java - 创建自定义控件以包含其他自定义控件

JavaFX:获取动态创建的按钮的ID

java - guice MapBinder 是否公开绑定(bind) key 以进行注入(inject)?

macos - 将 ManagedObjectContext 获取到初始 viewController OS X 的方法

JavaFx tableview 运行时异常

javafx - 如何锁定子阶段直到新的子阶段关闭

JavaFX 一次运行大量倒计时器?

c++ - 依赖倒置和普遍依赖

symfony - 将 SwiftMailer 注入(inject) symfony2 服务