gpt4 book ai didi

java - 使用 JavaFX 时出现 InspirationTargetException

转载 作者:行者123 更新时间:2023-12-02 02:29:56 25 4
gpt4 key购买 nike

我第一次尝试使用 Gluon/JavaFX,但我在运行时收到 InvocableTargetException。我已经在此网站和其他网站上搜索了答案,但在尝试每个建议后都收到了同样的问题。

主要:

package Scenes;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application{


public void start(Stage stage) throws Exception{

Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("asdf");
stage.show();
}

public static void main(String[] args) {

launch(args);
}



}

Controller :

package Scenes;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;

import javafx.scene.control.Button;


public class Controller {

Button kButton;
Button bpButton;

public void initialize() {

}

public static void browseFileK() {
JFileChooser jfc = new JFileChooser();
int returnValue = jfc.showOpenDialog(null);
if(returnValue == JFileChooser.APPROVE_OPTION)
{
File selectedFile = jfc.getSelectedFile();
}
}

public static void browseFileBP() {
JFileChooser jfc = new JFileChooser();
int returnValue = jfc.showOpenDialog(null);
if(returnValue == JFileChooser.APPROVE_OPTION)
{
File selectedFile = jfc.getSelectedFile();
}
}

}

FXML 文件:

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

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

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<AnchorPane cache="true" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style="&#10;" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField layoutX="21.0" layoutY="47.0" prefHeight="26.0" prefWidth="237.0" text="Select location of Kasasa accounts file" />
<TextField layoutX="21.0" layoutY="118.0" prefHeight="26.0" prefWidth="237.0" text="Select location of Bill Pay accounts file" />
<Button layoutX="294.0" layoutY="48.0" mnemonicParsing="false" text="Browse..." />
<Button layoutX="294.0" layoutY="115.0" mnemonicParsing="false" text="Browse..." />
<Button layoutX="168.0" layoutY="269.0" mnemonicParsing="false" prefHeight="32.0" prefWidth="253.0" text="Generate refund document" />
</children>
</AnchorPane>
</children>
</VBox>

堆栈跟踪:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
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 Scenes.Main.start(Main.java:14)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more

似乎最流行的建议是将fxml文件添加到与main相同的包中,在fxml文件前面添加“/”,或者将文件名更改为“Scenes/test.fxml”,无其中对我有用。

最佳答案

您问题的答案在很大程度上取决于您的构建系统正在做什么。例如,如果 .fxml 文件不在某个 resources 文件夹而不是源文件夹中,某些构建系统不会将它复制到构建目录中。其他人将需要明确指定要复制到构建文件夹的文件模式。

调试此问题的最快方法是检查您的构建文件夹(输出目录)并查看 .fxml 文件是否在那里。

关于java - 使用 JavaFX 时出现 InspirationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47287970/

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