3

我尝试在我的项目中使用 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

目录结构:

我在项目中拥有的文件

主班

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

}

登录视图类

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

登录.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>

聚甲醛

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

2 回答 2

5
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 文件中的包中。

于 2014-11-25T16:47:29.377 回答
3

这可能对原始海报没有帮助,因为这已经 10 个月大了,但我遇到了同样的问题。

我的 .fxml 在我的 jar 中的正确位置。我发现的解决方法是 fxml 文件名必须全部小写。

我的设置是 DevicePanelPresenter.java、DevicePanelView.java 和 devicePanel.fxml。这总是导致以下异常

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.getPresenter(FXMLView.java:175)
at com.zai.rackit.views.main.MainPresenter.initialize(MainPresenter.java:47)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)

追溯加力代码,找到了方法

String getConventionalName() {
    String clazz = this.getClass().getSimpleName().toLowerCase();
    return stripEnding(clazz);
}

所以我将我的 fxml 更改为 devicepanel.fxml,它现在可以工作了。

于 2015-09-25T22:09:36.917 回答