如何为我的 JavaFX 程序加载 FXML URL?
How do I load in an FXML URL for my JavaFX program?
我目前正在关注 JavaFX 的 Jenkov 教程。他写了下面的代码
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.net.URL;
public class FXMLExample extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(new URL("file:///C:/data/hello-world.fxml"));
VBox vbox = loader.<VBox>load();
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
}
这是他写的我的版本:
package sample;
import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.net.URL;
//import java.awt.*;
public class Main extends Application {
// The start method takes a single parameter of type stage.
// The stage is where all the visual parts of the application are displayed
// The stage is created
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(new URL("home/amnar/IdeaProjects/mockUI/src/sample/sample.fxml"));
VBox vbox = loader.<VBox>load();
Scene scene = new Scene(vbox);
primaryStage.setTitle("My first JavaFX app");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
现在,当我尝试 运行 时,出现以下堆栈跟踪错误:
Gtk-Message: 13:44:10.402: Failed to load module "canberra-gtk-module"
Exception in Application start method
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:498)
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(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$launchApplication4(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.MalformedURLException: no protocol: \home\amnar\IdeaProjects\mockUI\src\sample\sample.fxml
at java.net.URL.<init>(URL.java:593)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at sample.Main.start(Main.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null(GtkApplication.java:139)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
我知道 gtk 错误,但我不确定它是否导致了问题。我在我的机器上 运行ning ubuntu 18.04.01 LTS,budgie 作为桌面环境。我发现 gtk 错误是由默认安装为 32 位而我的机器为 64 位的原因引起的。我试图解决这个问题但无济于事。尽管如此,当我尝试 运行 JavaFX 示例而不必使用 FXML 文件时,即使错误仍然存在,它也能正常工作。
我在这里错过了什么?我是否必须做一些不同的事情,因为我是 运行宁 linux 而这个例子似乎是 windows 上的 运行?我见过各种其他导入 FXML 的方法,例如使用 getClass()... 等。同样,我会得到这个我不理解的巨大堆栈跟踪。 trace的临界区好像是这样的:
Caused by: java.net.MalformedURLException: no protocol: \home\amnar\IdeaProjects\mockUI\src\sample\sample.fxml
因此我认为我需要重新格式化 URL 但我不知道该怎么做。
谢谢!
您已经正确认识了您的问题。 Java 的 URL class 需要指定协议。试试这个:
loader.setLocation(new URL("file:///home/amnar/IdeaProjects/mockUI/src/sample/sample.fxml"));
我目前正在关注 JavaFX 的 Jenkov 教程。他写了下面的代码
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.net.URL;
public class FXMLExample extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(new URL("file:///C:/data/hello-world.fxml"));
VBox vbox = loader.<VBox>load();
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
}
这是他写的我的版本:
package sample;
import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.net.URL;
//import java.awt.*;
public class Main extends Application {
// The start method takes a single parameter of type stage.
// The stage is where all the visual parts of the application are displayed
// The stage is created
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(new URL("home/amnar/IdeaProjects/mockUI/src/sample/sample.fxml"));
VBox vbox = loader.<VBox>load();
Scene scene = new Scene(vbox);
primaryStage.setTitle("My first JavaFX app");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
现在,当我尝试 运行 时,出现以下堆栈跟踪错误:
Gtk-Message: 13:44:10.402: Failed to load module "canberra-gtk-module"
Exception in Application start method
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:498)
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(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$launchApplication4(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.MalformedURLException: no protocol: \home\amnar\IdeaProjects\mockUI\src\sample\sample.fxml
at java.net.URL.<init>(URL.java:593)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at sample.Main.start(Main.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null(GtkApplication.java:139)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
我知道 gtk 错误,但我不确定它是否导致了问题。我在我的机器上 运行ning ubuntu 18.04.01 LTS,budgie 作为桌面环境。我发现 gtk 错误是由默认安装为 32 位而我的机器为 64 位的原因引起的。我试图解决这个问题但无济于事。尽管如此,当我尝试 运行 JavaFX 示例而不必使用 FXML 文件时,即使错误仍然存在,它也能正常工作。
我在这里错过了什么?我是否必须做一些不同的事情,因为我是 运行宁 linux 而这个例子似乎是 windows 上的 运行?我见过各种其他导入 FXML 的方法,例如使用 getClass()... 等。同样,我会得到这个我不理解的巨大堆栈跟踪。 trace的临界区好像是这样的:
Caused by: java.net.MalformedURLException: no protocol: \home\amnar\IdeaProjects\mockUI\src\sample\sample.fxml
因此我认为我需要重新格式化 URL 但我不知道该怎么做。
谢谢!
您已经正确认识了您的问题。 Java 的 URL class 需要指定协议。试试这个:
loader.setLocation(new URL("file:///home/amnar/IdeaProjects/mockUI/src/sample/sample.fxml"));