我一直在做一个桌面应用程序,在那里我需要使用JavaFX。我在使用Scene Builder构建的eclipse上的JavaFX项目中创建了各种FXML文件。除了这个讨厌的文件(fxmlimglist.FXML)外,我加载任何FXML文件都没有问题。
其主要思想是,当按下一个按钮时,会出现一个新窗口。下面是该按钮的事件处理程序的代码:
@FXML
private void handleOnActionButtonImg(ActionEvent e) throws MalformedURLException, URISyntaxException
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/res/FXMLImgList.fxml").toURI().toURL());
VBox root = null;
try {
root = (VBox) loader.load(); // ERROR LOADING HERE
} catch (IOException e1) {
e1.printStackTrace();
}
ImgListController ilc = loader.getController();
ilc.init(imgUrl);
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner((Stage) vBox.getScene().getWindow());
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Elegir imagen");
stage.show();
}
package explorer中的项目如下所示:
https://image.ibb.co/ihgmos/capture.png(无法发布图像)
我从try-catch块中得到以下异常:
javafx.fxml.LoadException:
/C:/Users/Ximo/Desktop/UNI/2017-2018%20(UPV)/IPC/EclipseWorkspaceIPC/PL03_Ej2/bin/res/FXMLImgList.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.VentanaController.handleOnActionButtonImg(VentanaController.java:108)
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.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
at javafx.scene.image.Image.validateUrl(Image.java:1121)
at javafx.scene.image.Image.<init>(Image.java:659)
at application.ImgListController.initialize(ImgListController.java:64)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 61 more
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at javafx.scene.image.Image.validateUrl(Image.java:1115)
... 64 more
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)
at javafx.scene.Scene.<init>(Scene.java:194)
at application.VentanaController.handleOnActionButtonImg(VentanaController.java:116)
... 58 more
更奇怪的是,其余的FXML文件加载得很好,例如:
@FXML
private void handleOnActionButtonMod(ActionEvent event)
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/res/FXMLVentana.fxml"));
VBox root = null;
try {
root = (VBox) loader.load(); // NO ERROR LOADING HERE
} catch (IOException e) {
e.printStackTrace();
}
VentanaController vac = loader.getController();
vac.init(observablePersonData, table.getSelectionModel().selectedIndexProperty().getValue());
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(this.stage);
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Modificar");
stage.show();
}
这个事件处理程序背后的想法几乎是一样的,按下按钮->window打开并工作良好。我试过很多次,比如编辑返回的URL,但似乎都没用。
我注意到第67行的留言:
原因:java.lang.IllegalArgumentException:无效URL:未知协议:c
然而,结果是:
System.out.println(getClass().getResource("/res/FXMLVentana.fxml"));
System.out.println(getClass().getResource("/res/FXMLImgList.fxml"));
产出:
文件:/c:/users/ximo/desktop/uni/2017-2018%20(UPV)/ipc/eclipseworkspaceipc/pl03_ej2/bin/res/fxmlimglist.fxml
下面是fxmlimglist.fxml的代码:
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="12.0" xmlns=" http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ImgListController">
<children>
<HBox alignment="CENTER_RIGHT" spacing="12.0">
<children>
<Button fx:id="cancelar" mnemonicParsing="false" onAction="#handleOnActionButtonCancelar" text="Cancelar" />
<Button fx:id="salvar" mnemonicParsing="false" onAction="#handleOnActionButtonSalvar" text="Salvar" />
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</HBox>
</children>
<padding>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</padding>
</VBox>
下面是fxmlventana.fxml:
<?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.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="vBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="12.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.VentanaController">
<children>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="53.0" text="DNI" />
<TextField fx:id="dni" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="53.0" text="Nombre" />
<TextField fx:id="nombre" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="52.0" text="Apellidos" />
<TextField fx:id="apellidos" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="52.0" text="Ciudad" />
<TextField fx:id="city" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="30.0">
<children>
<Label prefHeight="17.0" prefWidth="52.0" text="Provincia" />
<TextField fx:id="province" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="12.0">
<children>
<ImageView fx:id="imgView" fitHeight="80.0" fitWidth="80.0" onMouseClicked="#handleImgMouseClick" pickOnBounds="true" preserveRatio ="true" />
<Button fx:id="buttonImg" mnemonicParsing="false" onAction="#handleOnActionButtonImg" text="Imagen" />
</children >
</HBox >
< HBox alignment="CENTER_RIGHT" spacing="12.0">
<children>
<Button fx:id="buttonCancelar" mnemonicParsing="false" onAction="#handleOnActionButtonCancelar" text="Cancelar" />
<Button fx:id="buttonAction" mnemonicParsing="false" onAction="#handleOnActionButtonAction" text="Salvar" />
</children>
</HBox>
</children>
<padding>
<Insets bottom="12.0" left="30.0" right="30.0" top="12.0" />
</padding>
</VBox>
总而言之,我的想法都没有了,而且我没有发现任何东西可以解决这个问题。感谢任何帮助,谢谢!
在stacktrace中有2个例外。
当您试图将root
传递给scene
的构造函数时,末尾的一个仅表示root
为空。这种情况发生在加载fxml失败之后。这并不是因为无效的fxml url,而是在imglistController.Initialize
方法中创建了一个image
作为构造函数参数的值,该值不是url的结果。您可能会传递一个绝对文件路径。
您可以使用file
或path
类获取文件的URL:
File f = ...
URL url = f.toURI().toURL();
Image img = new Image(url.toExternalForm());
注意:如果发生了异常,并且方法的其余部分依赖于try-block(在没有异常的情况下完成),您不应该简单地继续您的逻辑。这样你就得到了2个例外,而不是只有一个。
备选办法是:
>
重抛
我正在尝试使用以下代码将我的fxml文件集成到我的项目中, 程序在第二行崩溃,引发此异常, 也试过了, 我不知道这是否相关,但这是我的iml文件, 是什么原因造成的?我该如何修复它? 这里是我的项目的一个拉链,如果有人想看一下。 谢谢, 亨利
因此,在我尝试编写JavaFX教程和FXML示例时。但是,每当我在.fxml文件中向GridPane中添加一些内容时,程序就会崩溃。它打开一个普通的GridPane,如果没有其他东西放在其中。 } 究竟是什么原因导致它坠毁?
我正在用JavaFX构建一个应用程序,并为布局加载FXML文件。开始时,我的所有.fxml文件都在java类路径中。 由于我正在制作一个游戏引擎,所以我决定将所有的东西都移到类路径之外,所以我现在有了一个文件系统,如下所示: 这就是我加载FXML的方式 它看起来可能并不漂亮,但它可以工作(system.getProperty(“jdir”)只是.jar在光盘上的位置)。Java查找每个文件(FXM
我正在尝试创建javafx applet,使用IntelliJ idea。构建之后,我得到了三个文件:.jar、.jnlp和.html。如果我启动jar所有工作都很好,但是如果我尝试使用jnlp或html运行app,它会抛出异常: 为什么会这样?Jar正好包含所需的位于指定路径的fxml。 Java: FXML: 我做错了什么?请帮帮忙。
我的应用程序有选项卡式窗格,所以为了保持fxml文件的可管理性,我有一个包含选项卡的主fxml文件,并为每个其他选项卡单独设置一个fxml。这很好,但出于某种原因,应用程序已经停止加载第二个标签。我试着在主应用程序中单独加载它,效果很好。我尝试创建一个新的fxml文件作为测试,并加载它,这也是有效的。但是,它不会将其加载到第二个选项卡中。此外,控制台没有输出。 第二个: 很抱歉代码太长,并提前感谢
刚接触javaFx并希望使用scenebuilder进行GUI开发时,我遇到了一个问题,在网站上或整个web上搜索我的问题时都没有运气,尽管有人问过类似的问题,认为可能需要一个不同的视角。我试图在快速构建之后通过Netbeans加载一个FXML文件,以测试功能,这样代码就简单了,但是我无法在控制器中设置根文件。我的代码是下面的公共类Divergex extends Application{ 我尝试