gpt4 book ai didi

java - JavaFX Standalone JAR 能否获得对 Main java 类的 javascript 引用?

转载 作者:行者123 更新时间:2023-11-30 11:23:13 25 4
gpt4 key购买 nike

我预先为任何胆敢乱动这段代码的人道歉。我也提前谢谢你了!

我正在尝试启动一个简单的 JavaFX 独立 JAR 并从 javascript 访问我的 Java 类,以便从 javascript 调用 Java 函数。

我正在使用

document.getElementById("myApp");

但始终返回 null;

这是相关代码。我希望我没有遗漏任何重要内容。

构建.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="JavaFX Hello World" default="do-deploy" basedir="." xmlns:fx="javafx:com.sun.javafx.tools.ant">
<target name="init-fx-tasks">
<path id="fxant">
<filelist>
<file name="${java.home}\..\lib\ant-javafx.jar"/>
<file name="${java.home}\lib\jfxrt.jar"/>
</filelist>
</path>

<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpathref="fxant"/>
</target>
<target name="setup-staging-area">
<delete dir="externalLibs" />
<delete dir="project" />
<delete dir="projectRefs" />

<mkdir dir="externalLibs" />

<mkdir dir="project" />
<copy todir="project">
<fileset dir="C:\Development\workspace\JavaFX Hello World">
<include name="src/**" />
</fileset>
</copy>

<mkdir dir="projectRefs" />
</target>
<target name='do-compile'>
<delete dir="build" />
<mkdir dir="build/src" />
<mkdir dir="build/libs" />
<mkdir dir="build/classes" />

<!-- Copy project-libs references -->
<copy todir="build/libs">
<fileset dir="externalLibs">
</fileset>
</copy>

<!-- Copy project references -->

<!-- Copy project sources itself -->
<copy todir="build/src">
<fileset dir="project/src">
<include name="**/*"/>
</fileset>
</copy>

<javac includeantruntime="false" source="1.7" target="1.7" srcdir="build/src" destdir="build/classes" encoding="Cp1252">
<classpath>
<fileset dir="build/libs">
<include name="*"/>
</fileset>
</classpath>
</javac>

<!-- Copy over none Java-Files -->
<copy todir="build/classes">
<fileset dir="project/src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="do-deploy" depends="setup-staging-area, do-compile, init-fx-tasks">
<delete file="dist"/>
<delete file="dist/libs"/>
<mkdir dir="dist" />
<mkdir dir="dist/libs" />

<copy todir="dist/libs">
<fileset dir="externalLibs">
<include name="*" />
</fileset>
</copy>

<mkdir dir="build/classes/META-INF" />

<fx:deploy placeholderid="myApp" id="myApp"
width="640" height="480"
outdir="web-dist" outfile="Fish">
<fx:application id="myApp"
mainClass="application.ModuleSimMain">
</fx:application>
<fx:resources id="appRes">
<fx:fileset dir="dist" includes="Module Simulator Gui.jar"/>
<fx:fileset dir="dist" includes="libs/*"/>
</fx:resources>
</fx:deploy>

<fx:jar destfile="dist/Module Simulator Gui.jar">
<fx:application id="myApp"
mainClass="application.ModuleSimMain">
</fx:application>

<fx:resources id="appRes">
<fx:fileset dir="dist" includes="Module Simulator Gui.jar"/>
<fx:fileset dir="dist" includes="libs/*"/>
</fx:resources>

<fileset dir="build/classes"/>

<manifest>
<attribute name="Implementation-Vendor" value=""/>
<attribute name="Implementation-Title" value=""/>
<attribute name="Implementation-Version" value=""/>
<attribute name="JavaFX-Feature-Proxy" value="None"/>
</manifest>
</fx:jar>
</target>
</project>

这是我的主课;

application.ModuleSimMain.java

public class ModuleSimMain extends Application {
@Override
public void start(Stage primaryStage) {
try {
WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
final URL mainUrl = getClass().getResource("/content/ModuleSimulatorGui.html");
webEngine.setJavaScriptEnabled(true);
webEngine.load(mainUrl.toExternalForm());
Scene scene = new Scene(browser);
primaryStage.setTitle("Module Simulator - Protocol 2");
primaryStage.setScene(scene);
primaryStage.show();

webEngine.getLoadWorker().stateProperty()
.addListener(new ChangeListener<State>() {
@Override
public void changed( ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
System.out.println("succeeded");
webEngine.executeScript("ModuleSimulatorGui.init();");
}
}
});
} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
launch(args);
}

public String getText() {
return "success!";
}

content.ModuleSimulatorGui.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="ModuleSimulatorGui.css"/>
<script type="text/javascript" src="ModuleSimulatorGui.js"></script>
</head>
<body>
aofirwmnbrb<br/>
aofirwmnbrb<br/>
aofirwmnbrb<br/>
aofirwmnbrb<br/>
aofirwmnbrb<br/>
aofirwmnbrb<br/>
aofirwmnbrb<br/>
aofirwmnbrb<br/>
</body>
</html>

我省略了 CSS 文件,因为它只更改背景颜色并且有效。最后是我的 javascript。

content.ModuleSimulatorGui.js

var ModuleSimulatorGui = {
init: function() {
var fxapp = document.getElementById("myApp");
if( fxapp == null ) {
document.getElementsByTagName('body')[0].innerHTML = 'null';
} else {
document.getElementsByTagName('body')[0].innerHTML = fxapp.getText();
}
}
};

每当我构建并运行它,或从 Eclipse 运行它时,我都会得到一个大小和颜色正确的屏幕(来 self 的 CSS 文件),并带有“null”一词。

非常感谢任何帮助。

最佳答案

我找到了解决方法。忽略基于 build.xml 的说明。

https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx

我在我的主文件中添加了对我的应用程序的引用

final ModuleSimMain moduleSimMain = this;

然后在应用程序启动后运行的监听器中,我将对它的引用注入(inject)到 javascript 中。

webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed( ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
JSObject jsobj = (JSObject) webEngine.executeScript("window");
jsobj.setMember("myApp", moduleSimMain);
}
}
});

完成后我的javascript可以调用

myApp.myFunction();

成功了!

如果其他人遇到这个问题并且没有按照我所做的去做,请 ping 我。

关于java - JavaFX Standalone JAR 能否获得对 Main java 类的 javascript 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21342642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com