java - 如何将外部 JS 脚本导入到 FXML 布局?

标签 java javascript javafx-2 javafx fxml

我有 FXML 布局,我想添加外部 JS 文件(例如 HTTP 中的 src="http://blablabla.org/bla.js"),我尝试了以下操作:

<?xml version="1.0" encoding="UTF-8"?>
<?language javascript?>

<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml">
<children>
    <Button id="button" layoutX="126" layoutY="90" text="Click Me!" fx:id="button"
            onAction="changeButtonLabel(event);"/>
    <Label id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" prefHeight="16" prefWidth="69"
           fx:id="label"/>
</children>

<fx:script source="http://localhost:8080/TestApplication/main_script.js"/>

<stylesheets>
    <String fx:value="hive://localhost:8080/TestApplication/main.css" />
</stylesheets>
</AnchorPane>

但是 FXML 返回以下错误:

javafx.fxml.LoadException: javafx.fxml.LoadException: Base location is undefined.
at javafx.fxml.FXMLLoader$ScriptElement.processStartElement(FXMLLoader.java:1332)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)
at org.hive.browser.BrowserController$2.run(BrowserController.java:65)
Caused by: javafx.fxml.LoadException: Base location is undefined.
at javafx.fxml.FXMLLoader$ScriptElement.processStartElement(FXMLLoader.java:1314)
... 3 more
Exception in runnable
java.lang.NullPointerException: Children: child node is null: parent = Pane[id=contentPane]
at javafx.scene.Parent$1.onProposedChange(Parent.java:316)
at com.sun.javafx.collections.VetoableObservableList.add(VetoableObservableList.java:165)
at com.sun.javafx.collections.ObservableListWrapper.add(ObservableListWrapper.java:144)
at org.hive.browser.BrowserController$2$1.run(BrowserController.java:80)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)

我的代码有什么问题吗?

最佳答案

错误消息是“基本位置未定义”。我建议在 url 之前添加一个“@”,并将 javascript 放在相对于您的代码的某个路径中,例如@scripts/main_script.js,其中脚本是 Java 代码所在的目录。

关于java - 如何将外部 JS 脚本导入到 FXML 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19034078/

相关文章:

java - 项目运行时的 Eclipse 类加载顺序

javascript - JS 中读取 JSON 到变量时出错

JavaFX 8 文本字段表单元格

java - 将附加参数传递给 R 中 JDBCDriver 的 dbConnect 函数

java - 在二维数组中创建 JSON 来存储键值对

java - 在运行时使用 java.lang.annotation 检索 Spring 中的 bean

javascript - Jquery定时器倒计时和计数

javascript - 使用来自 GET 请求的 JSON 数组填充数据表

javafx-2 - XYChart 中的 Y 轴范围

javafx-2 - 访问 FXML Controller 类