所以这是我的 fxml 文件的文本代码:
我希望这个由 scenebuilder 打开,我已经设置了所有但它不起作用
每次我运行它时,要么他们要求我下载 javafx 但我做不到,要么就是不显示。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<GridPane alignment="center" hgap="10" vgap="10" padding="25" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="example.RegistrationFormController">
<Label text="Create New Account" GridPane.columnSpan="2" GridPane.rowIndex="0">
<font>
<Font size="18" />
</font>
</Label>
<Label text="Username:" GridPane.rowIndex="1" />
<TextField fx:id="usernameTextField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Email Address:" GridPane.rowIndex="2" />
<TextField fx:id="emailTextField" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Password:" GridPane.rowIndex="3" />
<PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label text="Confirm Password:" GridPane.rowIndex="4" />
<PasswordField fx:id="confirmPasswordField" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<HBox spacing="10" alignment="center" GridPane.columnSpan="2" GridPane.rowIndex="5">
<Button text="Create Account" onAction="#registerAccount" />
<Button text="Cancel" onAction="#cancelRegistration" />
</HBox>
<Label fx:id="errorLabel" textFill="#1c2280" GridPane.columnSpan="2" GridPane.rowIndex="6">
<padding>
<Insets top="10" />
</padding>
</Label>
</GridPane>
如果您尝试打开此文件是 SceneBuilder 它将显示一个对话框:
单击“显示详细信息...”会提供一个堆栈跟踪,告诉您出了什么问题。
java.io.IOException: javafx.fxml.LoadException:
/C:/Users/send2/sample.fxml:41
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.handleUnknownAndMissingCauses(FXOMLoader.java:119)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.handleKnownCauses(FXOMLoader.java:126)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.handleFxmlLoadingError(FXOMLoader.java:112)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.load(FXOMLoader.java:106)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.<init>(FXOMDocument.java:115)
at com.oracle.javafx.scenebuilder.kit.editor.EditorController.updateFxomDocument(EditorController.java:2561)
at com.oracle.javafx.scenebuilder.kit.editor.EditorController.setFxmlTextAndLocation(EditorController.java:764)
at com.oracle.javafx.scenebuilder.app.DocumentWindowController.loadFromFile(DocumentWindowController.java:398)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp.performOpenFiles(SceneBuilderApp.java:675)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp.handleOpenFilesAction(SceneBuilderApp.java:514)
at com.oracle.javafx.scenebuilder.app.welcomedialog.WelcomeDialogWindowController.handleOpen(WelcomeDialogWindowController.java:220)
at com.oracle.javafx.scenebuilder.app.welcomedialog.WelcomeDialogWindowController.openDocument(WelcomeDialogWindowController.java:215)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.reflect.Trampoline.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at javafx.base@18/com.sun.javafx.reflect.MethodUtil.invoke(Unknown Source)
at javafx.fxml@18/com.sun.javafx.fxml.MethodHelper.invoke(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at javafx.base@18/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base@18/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics@18/javafx.scene.Node.fireEvent(Unknown Source)
at javafx.controls@18/javafx.scene.control.Button.fire(Unknown Source)
at javafx.controls@18/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at javafx.controls@18/com.sun.javafx.scene.control.inputmap.InputMap.handle(Unknown Source)
at javafx.base@18/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base@18/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base@18/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics@18/javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.graphics@18/javafx.scene.Scene.processMouseEvent(Unknown Source)
at javafx.graphics@18/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at javafx.graphics@18/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at javafx.graphics@18/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at javafx.graphics@18/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
at javafx.graphics@18/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at javafx.graphics@18/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at javafx.graphics@18/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at javafx.graphics@18/com.sun.glass.ui.View.notifyMouse(Unknown Source)
at javafx.graphics@18/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@18/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/send2/sample.fxml:41
at javafx.fxml@18/javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader.load(Unknown Source)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.load(FXOMLoader.java:104)
... 62 more
Caused by: java.lang.IllegalArgumentException: Unable to coerce 25 to class javafx.geometry.Insets.
at javafx.fxml@18/com.sun.javafx.fxml.BeanAdapter.coerce(Unknown Source)
at javafx.fxml@18/com.sun.javafx.fxml.BeanAdapter.put(Unknown Source)
at javafx.fxml@18/com.sun.javafx.fxml.BeanAdapter.put(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader$Element.applyProperty(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader$Element.processValue(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader$Element.processPropertyAttribute(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader$ValueElement.processEndElement(Unknown Source)
at javafx.fxml@18/javafx.fxml.FXMLLoader.processEndElement(Unknown Source)
... 65 more
关键部分是:
java.io.IOException: javafx.fxml.LoadException:
/C:/Users/<username>/sample.fxml:41
. . .
Caused by: java.lang.IllegalArgumentException: Unable to coerce 25 to class javafx.geometry.Insets.
所以在 FXML 的第 41 行,解析器在指定 insets 时遇到了一个数字而不是 Insets 类型,因此无法解析文件并加载 SceneBuilder。 GridPane 是一个多行元素,直到第 41 行才会关闭,因此实际错误出现在 xml 标记开头的属性中,您有
padding="25"
.
在 FXML 的其他地方,使用标签,您已经以正确的方式指定了填充。要填充您的 GridPane,您需要类似的东西。
变化:
<GridPane alignment="center" hgap="10" vgap="10" padding="25" . . .
致:
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="example.RegistrationFormController">
<padding>
<Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
</padding>
. . .
</GridPane>
在此之后,您的 FXML 在 SceneBuilder 中正常打开(在 JavaFX Scene Builder 18.0.0 中测试)。