FXML 加载问题
FXML load issue
正在学习 fxml 的工作原理。
在 java tutorial 之后,我编写了一个似乎无法加载我的 fxml 文件的简单程序。
由于加载失败,我认为我的 GridPane 设置有问题。根据该教程的第 6-3 节,我认为它是正确的。
我也遵循了这个 和 运行 这个 fxml 文档的建议,我没有收到任何错误,并且确实出现了一个未格式化的白色框。我尝试将 xmlns:fx 变量调整为 http://javafx.com/fxml/1
但它并没有改变结果。
错误
C:\Users\rbenedict\Documents\Java Modules>java -cp .;fxmltut fxmltut.FXMLEx
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(Unk
nown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Sou
rce)
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(Unknown So
urce)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(
Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/rbenedict/Documents/Java%20Modules/fxmltut/FXMLDoc.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.importClass(Unknown Source)
at javafx.fxml.FXMLLoader.processImport(Unknown Source)
at javafx.fxml.FXMLLoader.processProcessingInstruction(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at fxmltut.FXMLEx.start(FXMLEx.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12
(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(Unknown
Source)
at com.sun.javafx.application.PlatformImpl.lambda$null3(Unknown Sourc
e)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(Unknown S
ource)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(Unknown Source)
... 1 more
Caused by: java.lang.ClassNotFoundException
at javafx.fxml.FXMLLoader.loadType(Unknown Source)
... 16 more
Exception running application fxmltut.FXMLEx
FXMLEx.java
package fxmltut;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Parent;
import java.lang.Class;
import javafx.fxml.FXMLLoader;
public class FXMLEx extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDoc.fxml"));
if(loader == null) {
System.out.println("Can't find fxml!");
System.exit(1);
}
Parent root = loader.load();
Scene scene = new Scene(root, 300,275);
stage.setTitle("FXML Welcome");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXMLCont
package fxmltut;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;
public class FXMLCont {
@FXML private Text actTarg;
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
actTarg.setText("Sign in button pressed");
}
}
FXMLDoc.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import fxmltut.*?>
<?import java.net.*?>
<?import java.geometry.*?>
<?import java.scene.control*?>
<?import java.scene.layout.*?>
<?import java.scene.text.*?>
<GridPane fx:controller="fxmltut.FXMLCont"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="25" left="25"/></padding>
<!--
<Text text="Welcome"
GridPane.columnIndex="0" GridPane.rowIndex="0"
GridPane.columnSpan="2"/>
<Label text="User Name:"
GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField
GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Password:"
GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<PasswordField fx:id="pw" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<HBox spacing="10" alignment = "bottom_right"
GridPane.columnIndex="1" GridPane.rowIndex="4">
<Button text="Sign in"
onAction="#handleSubmitButtonAction"/>
</HBox>
<Text fx:id="actTarg"
GridPane.columnIndex="0" GridPane.columnSpan="2"
GridPane.halignment="RIGHT" GridPane.rowIndex="6" />
-->
</GridPane>
工作 FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="377.0" prefWidth="533.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="fxmltut.FXMLCont">
</GridPane>
编辑
糟糕,我在粘贴 fxml 内容时忘记缩进导入语句。使线条消失。 St运行ge.
是的,如果您的 FXML 确实在类路径中并且有效,那么您应该没问题,但看起来它缺少导入语句。在 FXMLLoader 空检查之前,您还应该检查您的资源不会返回为空。您也许可以只使用静态 FXMLLoader.load 方法而不是实例化 FXMLLoader 对象。或者至少是一个 try-catch 块。
编辑:现在您已经修改了导入语句,它们指向 java. 等,而不是 javafx。应该是这样的...
<?import javafx.geometry.*?>
您在 FXML 文件中的导入不正确:
<?import java.geometry.*?>
<?import java.scene.control*?>
<?import java.scene.layout.*?>
<?import java.scene.text.*?>
成为
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
正在学习 fxml 的工作原理。
在 java tutorial 之后,我编写了一个似乎无法加载我的 fxml 文件的简单程序。
由于加载失败,我认为我的 GridPane 设置有问题。根据该教程的第 6-3 节,我认为它是正确的。
我也遵循了这个 http://javafx.com/fxml/1
但它并没有改变结果。
错误
C:\Users\rbenedict\Documents\Java Modules>java -cp .;fxmltut fxmltut.FXMLEx
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(Unk
nown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Sou
rce)
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(Unknown So
urce)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(
Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/rbenedict/Documents/Java%20Modules/fxmltut/FXMLDoc.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.importClass(Unknown Source)
at javafx.fxml.FXMLLoader.processImport(Unknown Source)
at javafx.fxml.FXMLLoader.processProcessingInstruction(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at fxmltut.FXMLEx.start(FXMLEx.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12
(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(Unknown
Source)
at com.sun.javafx.application.PlatformImpl.lambda$null3(Unknown Sourc
e)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(Unknown S
ource)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(Unknown Source)
... 1 more
Caused by: java.lang.ClassNotFoundException
at javafx.fxml.FXMLLoader.loadType(Unknown Source)
... 16 more
Exception running application fxmltut.FXMLEx
FXMLEx.java
package fxmltut;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Parent;
import java.lang.Class;
import javafx.fxml.FXMLLoader;
public class FXMLEx extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDoc.fxml"));
if(loader == null) {
System.out.println("Can't find fxml!");
System.exit(1);
}
Parent root = loader.load();
Scene scene = new Scene(root, 300,275);
stage.setTitle("FXML Welcome");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXMLCont
package fxmltut;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;
public class FXMLCont {
@FXML private Text actTarg;
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
actTarg.setText("Sign in button pressed");
}
}
FXMLDoc.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import fxmltut.*?>
<?import java.net.*?>
<?import java.geometry.*?>
<?import java.scene.control*?>
<?import java.scene.layout.*?>
<?import java.scene.text.*?>
<GridPane fx:controller="fxmltut.FXMLCont"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="25" left="25"/></padding>
<!--
<Text text="Welcome"
GridPane.columnIndex="0" GridPane.rowIndex="0"
GridPane.columnSpan="2"/>
<Label text="User Name:"
GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField
GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Password:"
GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<PasswordField fx:id="pw" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<HBox spacing="10" alignment = "bottom_right"
GridPane.columnIndex="1" GridPane.rowIndex="4">
<Button text="Sign in"
onAction="#handleSubmitButtonAction"/>
</HBox>
<Text fx:id="actTarg"
GridPane.columnIndex="0" GridPane.columnSpan="2"
GridPane.halignment="RIGHT" GridPane.rowIndex="6" />
-->
</GridPane>
工作 FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="377.0" prefWidth="533.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="fxmltut.FXMLCont">
</GridPane>
编辑
糟糕,我在粘贴 fxml 内容时忘记缩进导入语句。使线条消失。 St运行ge.
是的,如果您的 FXML 确实在类路径中并且有效,那么您应该没问题,但看起来它缺少导入语句。在 FXMLLoader 空检查之前,您还应该检查您的资源不会返回为空。您也许可以只使用静态 FXMLLoader.load 方法而不是实例化 FXMLLoader 对象。或者至少是一个 try-catch 块。
编辑:现在您已经修改了导入语句,它们指向 java. 等,而不是 javafx。应该是这样的...
<?import javafx.geometry.*?>
您在 FXML 文件中的导入不正确:
<?import java.geometry.*?>
<?import java.scene.control*?>
<?import java.scene.layout.*?>
<?import java.scene.text.*?>
成为
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>