我正在为大学设计JavaFX项目,我们必须在其中编程游戏“ Set”。我想测试我的代码并添加一些我已经设计的示例卡,但是一旦我将控制器添加到FXML文件中,它总是崩溃,没有该控制器,它可以正常工作,但是我只看到一个空白窗口。
这是我的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<fx:root fx:id="omg" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="980.0" prefWidth="1400.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.gui.testing">
<children>
<ImageView fx:id="card12" fitHeight="250.0" fitWidth="200.0" layoutX="1130.0" layoutY="690.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card11" fitHeight="250.0" fitWidth="200.0" layoutX="870.0" layoutY="690.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card10" fitHeight="250.0" fitWidth="200.0" layoutX="610.0" layoutY="690.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card9" fitHeight="250.0" fitWidth="200.0" layoutX="350.0" layoutY="690.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card8" fitHeight="250.0" fitWidth="200.0" layoutX="1130.0" layoutY="370.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card7" fitHeight="250.0" fitWidth="200.0" layoutX="870.0" layoutY="370.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card6" fitHeight="250.0" fitWidth="200.0" layoutX="610.0" layoutY="370.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card5" fitHeight="250.0" fitWidth="200.0" layoutX="350.0" layoutY="370.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card4" fitHeight="250.0" fitWidth="200.0" layoutX="1130.0" layoutY="50.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card3" fitHeight="250.0" fitWidth="200.0" layoutX="870.0" layoutY="50.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card2" fitHeight="250.0" fitWidth="200.0" layoutX="610.0" layoutY="50.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="card1" fitHeight="250.0" fitWidth="200.0" layoutX="350.0" layoutY="50.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="background" fitHeight="980.0" fitWidth="1400.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true" />
</children>
</fx:root>
这是我的控制者:
package test.gui;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class testing{
@FXML
ImageView card1;
@FXML
ImageView card2;
@FXML
ImageView card3;
@FXML
ImageView card4;
@FXML
ImageView card5;
@FXML
ImageView card6;
@FXML
ImageView card7;
@FXML
ImageView card8;
@FXML
ImageView card9;
@FXML
ImageView card10;
@FXML
ImageView card11;
@FXML
ImageView card12;
@FXML
ImageView background;
@FXML
AnchorPane omg;
public void addPictures() throws IOException {
card1.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/1.jpg")), null));
card2.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/2.jpg")), null));
card3.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/3.jpg")), null));
card4.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/4.jpg")), null));
card5.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/5.jpg")), null));
card6.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/6.jpg")), null));
card7.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/7.jpg")), null));
card8.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/8.jpg")), null));
card9.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/9.jpg")), null));
card10.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/10.jpg")), null));
card11.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/11.jpg")), null));
card12.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/cards/12.jpg")), null));
background.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("resources/background/background.jpg")), null));
}
}
我的主要班级:
public class Main extends Application {
;
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/gui/testing.fxml"));
primaryStage.setTitle("Set The Game!");
primaryStage.setScene(new Scene(root));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
这是我得到的错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at SetGame/test.Main.start(Main.java:16)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application test.Main
答案 0 :(得分:0)
尝试:
FXMLLoader.load(getClass().getClassLoader().getResource("/gui/testing.fxml"));
代替
FXMLLoader.load(getClass().getResource("/gui/testing.fxml"));
否则尝试从/ gui / test中删除第一个“ /”。......