[英]JavaFX: Location is not set
运行我的应用程序时,出现“应用程序启动方法[WARNING] 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有什么问题
码:
主要
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>
在main.java.resources上移动您的fxml
并使用Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
我编写了一个实用程序类来加载FXML文件,以减少此类错误。
如果您遵循一些命名约定,则可以使用以下方式加载FXML文件:
Parent root = FXMLLoaders.load(Main.class);
它加载<class name>.fxml
文件,该文件应与指定的类型位于同一包中,并设置:
<name>.properties
文件来查找ResourceBundle,其中<name>
等于指定类型的名称(或使用默认Locale的特定于语言环境的派生) 该库是开源的 ,可从Maven Central获得 :
<dependency>
<groupId>org.drombler.commons</groupId>
<artifactId>drombler-commons-fx-core</artifactId>
<version>0.13</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.