正如快速解释一样,我只是想设置一个包含两个GridPane的StackPane,一个在彼此之上。正如你可以从类名中判断出我正在尝试制作一款策略游戏,我希望有一个作为背景环境的GridPane,然后是一个存储播放器单元的GridPane。捕获所有点击事件。
我已经让StackPane加载没有问题,但后来我尝试在StackPane的initialize函数中加载环境GridPane和单元GridPane,并且我不断从中获取LoadException。我用路径尝试了一些不同的东西,但没有改变任何东西。
这是我目前所处的位置:
Main.java
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
StackPane root = FXMLLoader.load(getClass().getResource("/application/battleGrid/BattleGrid.fxml"));
primaryStage.setScene(new Scene(root, 700, 700));
primaryStage.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
BattleGridController.java package application.battleGrid;
import java.io.IOException;
import application.Main;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.LoadException;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
public class BattleGridController {
@FXML
private StackPane stack;
public void initialize() {
try {
GridPane environmentGrid = FXMLLoader.load(Main.class.getResource("/application/environmentGrid/EnvironmentGrid.fxml"));
GridPane unitGrid = FXMLLoader.load(Main.class.getResource("/application/unitGrid/UnitGrid.fxml"));
stack.getChildren().addAll(environmentGrid, unitGrid);
} catch (LoadException e) {
e.printStackTrace();
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BattleGrid.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.battleGrid.BattleGridController">
<!-- TODO Add Nodes -->
</StackPane>
UnitGrid.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<GridPane xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.unitGrid.UnitGridController">
</GridPane>
EnvironmentGrid.fxml
<?import javafx.scene.layout.GridPane?>
<GridPane xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.environmentGrid.EnvironmentGridController"
gridLinesVisible="true">
</GridPane>
控制器中目前没有任何环境和单元网格,所以那里应该没有问题。
这是堆栈跟踪:
javafx.fxml.LoadException:
/Users/markbluemer/Documents/workspace2/GridPanePractice/bin/application/battleGrid/BattleGrid.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:17)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
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.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 14 more
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.battleGrid.BattleGridController.initialize(BattleGridController.java:19)
... 25 more
一般项目结构:
编辑:
更改
FXMLLoader.load(Main.class.getResource("/application/unitGrid/unitGrid.fxml"));
到
FXMLLoader.load(Main.class.getResource("/application/unitGrid/UnitGrid.fxml"));
同样的问题仍然存在
答案 0 :(得分:0)
您的代码正在加载此内容:
Main.class.getResource("/application/unitGrid/unitGrid.fxml")
但是你说它的名字有一个大写的情况&#34; U&#34;
UnitGrid.fxml
尝试更改为区分大小写的加载。
答案 1 :(得分:0)
堆栈跟踪指示未设置位置。这意味着FXMLLoader找不到资源路径。正如咕噜咕噜的鸽子所做的那样,做出了这些改变。
<强>更新强>
要加载资源,请使用CurrentThread,
Thread.currentThread().getContextClassLoader().getResource("fxml_file_path")