我目前正在关注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$launchApplication$154(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$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.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
我知道gtk错误,并且不确定是否是引起问题的原因。我在将budgie作为桌面环境的计算机上运行ubuntu 18.04.01 LTS。我发现gtk错误是由某些原因导致的,默认安装为32位,而我的计算机为64位。我试图解决此问题无济于事。但是,当我尝试运行JavaFX示例而不必使用FXML文件时,即使错误仍然存在,它也可以正常工作。
我在这里想念什么?我是否正在运行linux,而示例似乎在Windows上运行,我是否需要做一些其他的事情?我已经看到了其他各种导入FXML的方式,例如使用getClass()等。同样,我会得到这个巨大的堆栈跟踪信息,我不理解。跟踪的关键部分似乎是这样的:
Caused by: java.net.MalformedURLException: no protocol: \home\amnar\IdeaProjects\mockUI\src\sample\sample.fxml
因此,我认为我需要重新格式化URL,但是我不知道该怎么做。
谢谢!
答案 0 :(得分:2)
您已正确识别您的问题。 Java的URL类需要具有指定的协议。试试看:
loader.setLocation(new URL("file:///home/amnar/IdeaProjects/mockUI/src/sample/sample.fxml"));