Java API 用于在桌面应用程序中操作 html/Javascript DOM 元素?
Java API for manipulating html/Javascript DOM elements in a desktop application?
现有的 HTML 个元素在 H:
下给出
https://developer.mozilla.org/de/docs/Web/API
此处显示可用的 JAVA API 类:
https://xerces.apache.org/xerces2-j/javadocs/api/org/w3c/dom/Element.html
不幸的是,org.w3c.dom 包缺少一些 更具体的元素 ,例如 HTMLCanvasElement.
我正在 寻找更高级的 Java API 至少支持 HTMLCanvasElements(完整的 WEP API 支持就好了)。
到目前为止我发现了什么:
一个。对于项目 javafx-d3 I use the JavaFx WebView to control some JavaScript in Java. The communication between Java and JavaScript is based on netscape.javascript.JSObject 并且原则上工作正常:
我能够将从 JavaScript 世界获得的 JSObject 投射到 org.w3c.dom.Element ... 并使用它的 Java 方法来操作DOM。
如果我想"go deeper",例如控制一个 HTMLCanvasElement,我必须基于 JSObject 编写自己的包装器。这是可能的,但感觉就像重新发明轮子。
乙。 Google GWT 似乎提供了一些包装器 类,例如
com.google.gwt.dom.client.CanvasElement for HTMLCanvasElement
但是我没有设法将 netscape.javascript.JSObject 转换为 GWT 包装器 类(从 com.google.gwt.core.client.JavaScriptObject 开始)。
如果这完全可行,有人可以提供示例吗?
有人知道如何获得下面的虚拟示例吗运行?
package main;
import elemental.client.Browser;
import elemental.html.AudioContext;
import elemental.html.AudioParam;
import elemental.html.Oscillator;
import elemental.html.Window;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class GwtElementDemo extends Application {
private Scene scene;
private Region browser;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
//set state title
stage.setTitle("JavaFx demo");
browser = new Region();
Window window = Browser.getWindow();
AudioContext audioContext = window.newAudioContext();
Oscillator osc = audioContext.createOscillator();
osc.setType(Oscillator.SQUARE);
osc.connect((AudioParam) audioContext.getDestination(), 0);
osc.getFrequency().setValue(440.0f);
osc.noteOn(0);
//create the scene
scene = new Scene(browser, 750, 500, Color.web("#666970"));
stage.setScene(scene);
stage.show();
}
}
GWT 元素的 Maven 依赖项:
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-elemental</artifactId>
<version>2.8.0</version>
</dependency>
或者您知道 alternative/advanced JAVA API 用于 DOM 操作(构建在 JSObject 层次结构上)吗?
是的,有一个,与您已经找到的非常接近。它被称为 GWT 元素。虽然它是 GWT,但它确实直接在 DOM 上,令人难以置信的完整并且真的非常像直接在 DOM 上的如履薄冰。我正在使用它在 https://github.com/nielsbaloe/vertxui 编写 100% java 100% 异步框架 VertxUI。
我就是那个试图将 Elemental 移植到您在评论中提到的 JavaFx 的人。我用它制作了一个名为 Animation Companion 的小应用程序,但它并不是那么好。 JavaFx 提供的 WebKit 引擎缺少很多功能,并且将大量数据从 Java 移动到 JavaScript 并返回非常慢。 JavaFx 人员重写了 WebKit 以依赖于 JavaFx 进行渲染,有时会出现一些不匹配,这会导致崩溃或 运行 如果你推送资源UI太难了。此外,Elemental 本身有点 out-dated,具有很多 WebKit-specific 功能、缺少的功能和其他错误。
来自我的 github 的测试 JavaFx 代码要求您先构建元素。我不完全记得该怎么做。我认为您只需进入元素目录并在那里 运行 ant
,然后您会在某处得到一个 gwt-elemental.jar
文件输出,您可以 link 对其进行测试。我暂时在 http://www.user00.com/gwt-elemental.jar . You also need a few files from gwt-user.jar
from GWT 2.7. I've temporarily made those files available at http://www.user00.com/gwt-user-pruned.jar 放了一个 pre-built 版本。从那里,您可以使用如下内容启动网页:
Stage stage = ...
// Create the WebView
BorderPane border = new BorderPane();
WebView webView = new WebView();
final WebEngine engine = webView.getEngine();
border.setCenter(webView);
Scene scene = new Scene(border);
stage.setScene(scene);
stage.show();
// Redirect the alert handler to send output to stderr
engine.setOnAlert(new EventHandler<WebEvent<String>>() {
@Override
public void handle(WebEvent<String> msg) {
System.err.println(msg.getData());
}});
// Start up the Gwt module when the page has finished loading. Grab the window
// and document objects and stash them somewhere for use later.
engine.getLoadWorker().stateProperty().addListener(
new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends State> ov,
State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
Window win = (Window)GwtFxBridge.wrapJs(engine.executeScript("window"));
// The web page is loaded, and you have the global window
// JS object, so you can start your UI now
}
}
});
// Load the main Gwt application web page
try {
engine.load(new File("ui/index.html").toURI().toURL().toExternalForm());
}
catch(MalformedURLException e)
{
throw new IllegalArgumentException("Misconfiguration error. Cannot find empty web page", e);
}
我真的不建议走这条路。只是没有足够的支持,你必须非常了解你的 JavaScript 和你的 Java 才能调试你将面临的问题。我认为颠倒事物更有意义。 运行 nw.js 到 运行 Java脚本作为驱动应用程序的主要 VM,然后在需要时调用 Java Java事物。您甚至可以使用 GWT 从 Java 代码生成 JavaScript。
现有的 HTML 个元素在 H:
下给出https://developer.mozilla.org/de/docs/Web/API
此处显示可用的 JAVA API 类:
https://xerces.apache.org/xerces2-j/javadocs/api/org/w3c/dom/Element.html
不幸的是,org.w3c.dom 包缺少一些 更具体的元素 ,例如 HTMLCanvasElement.
我正在 寻找更高级的 Java API 至少支持 HTMLCanvasElements(完整的 WEP API 支持就好了)。
到目前为止我发现了什么:
一个。对于项目 javafx-d3 I use the JavaFx WebView to control some JavaScript in Java. The communication between Java and JavaScript is based on netscape.javascript.JSObject 并且原则上工作正常:
我能够将从 JavaScript 世界获得的 JSObject 投射到 org.w3c.dom.Element ... 并使用它的 Java 方法来操作DOM。
如果我想"go deeper",例如控制一个 HTMLCanvasElement,我必须基于 JSObject 编写自己的包装器。这是可能的,但感觉就像重新发明轮子。
乙。 Google GWT 似乎提供了一些包装器 类,例如
com.google.gwt.dom.client.CanvasElement for HTMLCanvasElement
但是我没有设法将 netscape.javascript.JSObject 转换为 GWT 包装器 类(从 com.google.gwt.core.client.JavaScriptObject 开始)。
如果这完全可行,有人可以提供示例吗?
有人知道如何获得下面的虚拟示例吗运行?
package main;
import elemental.client.Browser;
import elemental.html.AudioContext;
import elemental.html.AudioParam;
import elemental.html.Oscillator;
import elemental.html.Window;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class GwtElementDemo extends Application {
private Scene scene;
private Region browser;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
//set state title
stage.setTitle("JavaFx demo");
browser = new Region();
Window window = Browser.getWindow();
AudioContext audioContext = window.newAudioContext();
Oscillator osc = audioContext.createOscillator();
osc.setType(Oscillator.SQUARE);
osc.connect((AudioParam) audioContext.getDestination(), 0);
osc.getFrequency().setValue(440.0f);
osc.noteOn(0);
//create the scene
scene = new Scene(browser, 750, 500, Color.web("#666970"));
stage.setScene(scene);
stage.show();
}
}
GWT 元素的 Maven 依赖项:
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-elemental</artifactId>
<version>2.8.0</version>
</dependency>
或者您知道 alternative/advanced JAVA API 用于 DOM 操作(构建在 JSObject 层次结构上)吗?
是的,有一个,与您已经找到的非常接近。它被称为 GWT 元素。虽然它是 GWT,但它确实直接在 DOM 上,令人难以置信的完整并且真的非常像直接在 DOM 上的如履薄冰。我正在使用它在 https://github.com/nielsbaloe/vertxui 编写 100% java 100% 异步框架 VertxUI。
我就是那个试图将 Elemental 移植到您在评论中提到的 JavaFx 的人。我用它制作了一个名为 Animation Companion 的小应用程序,但它并不是那么好。 JavaFx 提供的 WebKit 引擎缺少很多功能,并且将大量数据从 Java 移动到 JavaScript 并返回非常慢。 JavaFx 人员重写了 WebKit 以依赖于 JavaFx 进行渲染,有时会出现一些不匹配,这会导致崩溃或 运行 如果你推送资源UI太难了。此外,Elemental 本身有点 out-dated,具有很多 WebKit-specific 功能、缺少的功能和其他错误。
来自我的 github 的测试 JavaFx 代码要求您先构建元素。我不完全记得该怎么做。我认为您只需进入元素目录并在那里 运行 ant
,然后您会在某处得到一个 gwt-elemental.jar
文件输出,您可以 link 对其进行测试。我暂时在 http://www.user00.com/gwt-elemental.jar . You also need a few files from gwt-user.jar
from GWT 2.7. I've temporarily made those files available at http://www.user00.com/gwt-user-pruned.jar 放了一个 pre-built 版本。从那里,您可以使用如下内容启动网页:
Stage stage = ...
// Create the WebView
BorderPane border = new BorderPane();
WebView webView = new WebView();
final WebEngine engine = webView.getEngine();
border.setCenter(webView);
Scene scene = new Scene(border);
stage.setScene(scene);
stage.show();
// Redirect the alert handler to send output to stderr
engine.setOnAlert(new EventHandler<WebEvent<String>>() {
@Override
public void handle(WebEvent<String> msg) {
System.err.println(msg.getData());
}});
// Start up the Gwt module when the page has finished loading. Grab the window
// and document objects and stash them somewhere for use later.
engine.getLoadWorker().stateProperty().addListener(
new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends State> ov,
State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
Window win = (Window)GwtFxBridge.wrapJs(engine.executeScript("window"));
// The web page is loaded, and you have the global window
// JS object, so you can start your UI now
}
}
});
// Load the main Gwt application web page
try {
engine.load(new File("ui/index.html").toURI().toURL().toExternalForm());
}
catch(MalformedURLException e)
{
throw new IllegalArgumentException("Misconfiguration error. Cannot find empty web page", e);
}
我真的不建议走这条路。只是没有足够的支持,你必须非常了解你的 JavaScript 和你的 Java 才能调试你将面临的问题。我认为颠倒事物更有意义。 运行 nw.js 到 运行 Java脚本作为驱动应用程序的主要 VM,然后在需要时调用 Java Java事物。您甚至可以使用 GWT 从 Java 代码生成 JavaScript。