JavaFX:未设置位置

JavaFX:未设置位置,java,maven,javafx,java-11,Java,Maven,Javafx,Java 11,当我运行我的应用程序时,我在应用程序启动方法[警告]中遇到“异常” java.lang.reflect.InvocationTargetException”。我尝试使用fxml路径,但仍然不起作用。使用try/catch时,我得到“位置未设置错误” 我也有 “无法在项目测试生成器maven上执行目标org.codehaus.mojo:exec maven插件:1.2.1:java(默认cli):执行java类时发生异常。null:InvocationTargetException:应用程序启动方

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

我也有 “无法在项目测试生成器maven上执行目标org.codehaus.mojo:exec maven插件:1.2.1:java(默认cli):执行java类时发生异常。null:InvocationTargetException:应用程序启动方法中的异常:需要位置。” 但是我不知道我的pom.xml有什么问题

代码:

主要

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>

4.0.0
com.mateuszm
测试生成器Maven
1.0-快照
UTF-8
org.openjfx
javafx控件
11
org.openjfx
javafx媒体
11
赢
org.openjfx
javafx媒体
11
雨衣
org.openjfx
javafx媒体
11
linux
org.openjfx
javafx图形
11
赢
org.openjfx
javafx图形
11
雨衣
org.openjfx
javafx图形
11
linux
org.openjfx
javafx-fxml
11
org.apache.maven.plugins
maven编译器插件
3.8.0
11
11
org.codehaus.mojo
execmaven插件
1.2.1
JAVA
com.mateuszm.main.main

在main.java.resources上移动fxml

和使用
Parent root=fxmloader.load(getClass().getResource(“MainWindow.fxml”)

我已经编写了一个用于加载FXML文件的程序,以减少此类错误

如果您坚持某些命名约定,则可以加载带有以下内容的FXML文件:

Parent root = FXMLLoaders.load(Main.class);
它加载
.fxml
文件,该文件应与指定类型位于同一个包中,并设置:

  • 将类加载器添加到指定类型的类加载器
  • 通过在同一个包中查找
    .properties
    文件,其中
    等于指定类型的名称(或使用默认区域设置的特定于区域设置的派生),可以创建ResourceBundle
  • FXML文件的位置
图书馆可从以下网址获得:


org.drombler.commons
drombler commons fx核心
0.13

在getResource中添加fxml的完整路径。getResource(“/main/java/com/mateuszm/views/MainWindow.fxml”);您的FXML文件位于
src/main/java
下,但它需要位于
src/main/resources
下。Maven不在
src/main/java
下处理资源。仍然是一样的。我用pom.xml更新我的ask代码
getResource
不允许“向上导航”:如果资源不在同一个包或子包中,则需要传递从类路径根开始的完整路径:
getResource(“/com/mateuszm/views/MainWindow.fxml”)
pom仍然相同,可能有什么问题?我不知道
Parent root = FXMLLoaders.load(Main.class);
<dependency>
  <groupId>org.drombler.commons</groupId>
  <artifactId>drombler-commons-fx-core</artifactId>
  <version>0.13</version>
</dependency>