gpt4 book ai didi

JavaFX:位置未设置

转载 作者:太空宇宙 更新时间:2023-11-04 09:25:24 25 4
gpt4 key购买 nike

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

我也有“无法在项目 Test-generator-Maven 上执行目标 org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli):执行 Java 类时发生异常。null:InitationTargetException:应用程序启动方法中出现异常:需要位置。”但我不知道我的 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>

最佳答案

将您的 fxml 移至 main.java.resources

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

关于JavaFX:位置未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57757278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com