【问题标题】:JavaFX: Location is not setJavaFX:未设置位置
【发布时间】:2019-09-02 12:52:05
【问题描述】:

当我运行我的应用程序时,出现“应用程序启动方法中的异常 [警告] java.lang.reflect.InvocationTargetException”。我尝试使用 fxml 路径但仍然无法正常工作。使用 try/catch 时出现“位置未设置错误”。

我也有 “无法在项目 Test-generator-Maven 上执行目标 org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli):执行 Java 类时发生异常。null:InvocationTargetException:异常应用启动方式:位置为必填项。” 但我不知道我的 pom.xml 出了什么问题

My project structure

代码:

主要

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("../views/MainWindow.fxml"));
        Scene scene = new Scene(root, 900, 400);
        stage.setTitle("Generator testów");
        stage.setScene(scene);
        stage.show();

    }

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

MainWindow.fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>


<GridPane prefHeight="414.0" prefWidth="864.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mateuszm.controllers.MainWindowController">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="252.0" minWidth="10.0" prefWidth="252.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="610.4000122070313" minWidth="10.0" prefWidth="503.20000000000005" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
    </rowConstraints>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mateuszm</groupId>
    <artifactId>Test-generator-Maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>11</version>
            <classifier>win</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>11</version>
            <classifier>mac</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>11</version>
            <classifier>linux</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>11</version>
            <classifier>win</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>11</version>
            <classifier>mac</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>11</version>
            <classifier>linux</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.mateuszm.main.Main</mainClass>
                </configuration>
            </plugin>


        </plugins>
    </build>


</project>

【问题讨论】:

  • 在getResource中添加fxml的完整路径。 .getResource("/main/java/com/mateuszm/views/MainWindow.fxml"));
  • 您的 FXML 文件位于 src/main/java 下,但它必须位于 src/main/resources 下。 Maven 不处理src/main/java 下的资源。
  • 还是一样。我用 pom.xml 代码更新我的问题
  • getResource 不允许“向上导航”:如果资源不在同一个包或子包中,则需要传递从类路径根开始的完整路径:getResource("/com/mateuszm/views/MainWindow.fxml")
  • 还是一样,可能是 pom 有问题?我不知道

标签: java maven javafx java-11


【解决方案1】:

在 main.java.resources 上移动你的 fxml

并使用 Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));

【讨论】:

    【解决方案2】:

    我写了一个utiltiy class 用于加载 FXML 文件以减少此类错误。

    如果你坚持一些命名约定,你可以加载一个 FXML 文件:

    Parent root = FXMLLoaders.load(Main.class);
    

    它加载&lt;class name&gt;.fxml文件,该文件预计与指定类型在同一个包中,并设置:

    • ClassLoader到指定类型的ClassLoader
    • ResourceBundle 通过在同一包中查找&lt;name&gt;.properties 文件,其中&lt;name&gt; 等于指定类型的名称(或使用默认语言环境的特定语言环境派生)
    • FXML 文件的位置

    图书馆是Open Source,可从Maven Central获得:

    <dependency>
      <groupId>org.drombler.commons</groupId>
      <artifactId>drombler-commons-fx-core</artifactId>
      <version>0.13</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 2013-06-18
      相关资源
      最近更新 更多