尝试使用javafx&fxml,但由于invocationtargetexceptions和nullexceptions,程序无法运行

798qvoo8  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(406)

这个问题在这里已经有了答案

如何确定我的javafx应用程序所需的fxml文件、css文件、图像和其他资源的正确路径(1个答案)
上个月关门了。
我正在尝试学习eclipse中的javafx,并且已经正确安装了它。因为我不想编写太多的布局代码,因为它很耗时,所以我发现有一种方法可以对gui建模,那就是fxml。然而,当我试图编写我的fxml时,它却导致了一个错误。参考图片:


我有这些文件:main

package application;

import java.io.FileInputStream;
import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application{

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception{
        FXMLLoader loader = new FXMLLoader();
        String fxmlDocPath = "/fxmltest/src/main.fxml";
        FileInputStream fxmlStream = new FileInputStream(fxmlDocPath);
        AnchorPane root = (AnchorPane) loader.load(fxmlStream);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Example");
        stage.show();

    }

}

fxml公司

<?xml version="1.0" encoding="UTF-8"?>

<?import com.gluonhq.charm.glisten.control.TextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="239.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" />
      <Text fx:id="title" layoutX="13.0" layoutY="34.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Scene Builder Sample" wrappingWidth="574.13671875">
         <font>
            <Font name="Corbel" size="27.0" />
         </font>
      </Text>
      <Text fx:id="nameDesc" layoutX="13.0" layoutY="54.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Enter your name below:" wrappingWidth="574.13671875" />
      <TextField fx:id="nameField" layoutX="14.0" layoutY="64.0" prefHeight="26.0" prefWidth="496.0" promptText="Enter your name!" />
      <Button fx:id="enter" layoutX="530.0" layoutY="61.0" mnemonicParsing="false" text="Enter" />
      <Text fx:id="expecDesc" layoutX="14.0" layoutY="119.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Enter your expectations below:" wrappingWidth="574.13671875" />
      <TextField fx:id="expecField" layoutX="15.0" layoutY="129.0" prefHeight="26.0" prefWidth="496.0" promptText="Enter your name!" />
      <Text fx:id="resultsfield" layoutX="14.0" layoutY="174.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Here are the results." wrappingWidth="574.13671875" />
      <Text fx:id="nameLabel" layoutX="13.0" layoutY="191.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Name:" wrappingWidth="42.98307228088379" />
      <Text fx:id="expecLabel" layoutX="13.0" layoutY="215.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Results:" wrappingWidth="54.0" />
      <Text fx:id="nameShow" layoutX="67.0" layoutY="191.0" strokeType="OUTSIDE" strokeWidth="0.0" text=" " wrappingWidth="474.9830722808838" />
      <Text fx:id="expecShow" layoutX="67.0" layoutY="215.0" strokeType="OUTSIDE" strokeWidth="0.0" text=" " wrappingWidth="474.9830722808838" />
   </children>
</AnchorPane>

错误代码是:

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:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    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:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
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:832)
Caused by: java.io.FileNotFoundException: \fxmltest\src\main.fxml (The system cannot find the path specified)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:108)
    at application.Main.start(Main.java:22)
    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:391)
    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 application.Main

我在eclipse 2020-09中运行这个程序,并从这里开始关注javafx安装:https://www.youtube.com/watch?v=bc4xb6jaaou. 那么如何呈现这个fxml呢?

wmomyfyw

wmomyfyw1#

右键单击main.fxml文件并获取相对路径。复制并粘贴到下面的代码中。。

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{

    Parent root = FXMLLoader.load(getClass().getResource("/relative_path_to/main.fxml"));
    primaryStage.setTitle("Login");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

相关问题