FXML 文件导致应用打不开
FXML file causes the app to not open up
我正在学习 PluralSite 上的这门课程,我一直在复制代码,但是他使用的是 netbeans,而我使用的是 eclipse,我无法弄清楚发生了什么。使用netbeans和ecplise使用fxml文件有区别吗?如果有人可以提供帮助,我会很高兴。如果您需要更多信息,请务必询问。我是 javafx 的新手,所以很好。 fxml文件与其他文件在同一个包中:
这是我得到的异常,然后是应用程序代码,然后是 fxml 代码
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$launchApplication5(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 application.SimpleUI.start(SimpleUI.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
... 1 more
Exception running application application.SimpleUI
//first is my app
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SimpleUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));
// VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down"));
//
// box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox);
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
//this is my fxml code
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SimpleUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));
// VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down"));
//
// box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox);
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import java.scene.*?>
<?import java.scene.control.*?>
<?import java.scene.layout.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<HBox>
<children>
<Label text= "UserName" />
<TextField />
<Button text= "Connect" />
</children>
</HBox>
修复:感谢回答我问题的人,我修复了它
我不确定这是否会解决您的问题,但我看到加载 fxml 文件的典型方式与您发布的代码之间的一个区别是加载程序中的路径,即:
HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));
我通常这样加载 fxml:
AnchorPane page = (AnchorPane) FXMLLoader.load(PreloadController.class.getResource("PreloaderGUI.fxml"));
并确保我的 Controller
和 fxml 文件在同一个包中。我假设 getResource
方法在这些示例之间是相同的,但传递的 String
路径不同。尝试将其更改为 fxml 文件的简单名称,看看它是否有效。即如下:
HBox box = FXMLLoader.load(getClass().getResource("simpleui.fxml"));
我正在学习 PluralSite 上的这门课程,我一直在复制代码,但是他使用的是 netbeans,而我使用的是 eclipse,我无法弄清楚发生了什么。使用netbeans和ecplise使用fxml文件有区别吗?如果有人可以提供帮助,我会很高兴。如果您需要更多信息,请务必询问。我是 javafx 的新手,所以很好。 fxml文件与其他文件在同一个包中:
这是我得到的异常,然后是应用程序代码,然后是 fxml 代码
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$launchApplication5(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 application.SimpleUI.start(SimpleUI.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
... 1 more
Exception running application application.SimpleUI
//first is my app
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SimpleUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));
// VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down"));
//
// box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox);
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
//this is my fxml code
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SimpleUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));
// VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down"));
//
// box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox);
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import java.scene.*?>
<?import java.scene.control.*?>
<?import java.scene.layout.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<HBox>
<children>
<Label text= "UserName" />
<TextField />
<Button text= "Connect" />
</children>
</HBox>
修复:感谢回答我问题的人,我修复了它
我不确定这是否会解决您的问题,但我看到加载 fxml 文件的典型方式与您发布的代码之间的一个区别是加载程序中的路径,即:
HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));
我通常这样加载 fxml:
AnchorPane page = (AnchorPane) FXMLLoader.load(PreloadController.class.getResource("PreloaderGUI.fxml"));
并确保我的 Controller
和 fxml 文件在同一个包中。我假设 getResource
方法在这些示例之间是相同的,但传递的 String
路径不同。尝试将其更改为 fxml 文件的简单名称,看看它是否有效。即如下:
HBox box = FXMLLoader.load(getClass().getResource("simpleui.fxml"));