仅使用JavaScript(Nashorn)进行JavaFX开发 [英] JavaFX development with just JavaScript (Nashorn)
问题描述
现在Nashorn是否可以在Java 8中使用,是否有办法用JavaScript文件编写JavaFX应用程序?我在哪里可以找到更多关于此的信息或视频?你会如何编译它?
Is there a way to write a JavaFX app with just JavaScript files now that Nashorn is available in Java 8? Where can I find more info or videos on this? How would you compile it?
或者是否需要一组最小的bootstrapping文件?
Or is there a minimal set of bootstrapping files required?
推荐答案
是的,你可以用JavaScript(Nashorn)开发JavaFX程序。
Yes, you can develop JavaFX programs with just JavaScript (Nashorn).
如何从JavaScript访问JavaFX
使用 jjs -fx yourscript.js
命令行开关。
-fx 标志8 / docs / technotes / tools / windows / jjs.html> jjs
将使用 javafx.application.Application
。
The -fx
flag on jjs
will bootstrap scripts using a javafx.application.Application
.
JavaScript没有编译步骤,只需编写脚本并使用 jjs -fx
运行它。
There is no compile step for JavaScript, just write your script and run it with jjs -fx
.
不需要bootstrapping文件。
There are no bootstrapping files required.
jjs
二进制文件和JavaFX运行时都是随Oracle Java 8运行时环境(JRE)和 jjs
一起提供,它位于JRE安装的bin目录中。
The jjs
binary and the JavaFX runtime are both shipped with the Oracle Java 8 Runtime Environment (JRE) and jjs
is located in the bin directory of the JRE installation.
示例代码
一个Hello World示例(复制自 Jim Laskey的博客)。
A Hello World example (copied from Jim Laskey's blog).
var Button = Java.type("javafx.scene.control.Button");
var StackPane = Java.type("javafx.scene.layout.StackPane");
var Scene = Java.type("javafx.scene.Scene");
$STAGE.title = "Hello World!";
var button = new Button();
button.text = "Say 'Hello World'";
button.onAction = function() print("Hello World!");
var root = new StackPane();
root.children.add(button);
$STAGE.scene = new Scene(root, 300, 250);
$STAGE.show();
其他资源
有许多使用Nashorn脚本运行JavaFX的Web示例。
There are numerous web examples of running JavaFX using Nashorn scripts.
- JavaFX与Nashorn Hello World和3D演示
- JavaFX与Nashorn Canvas示例。
- 使用Nashorn Animated Gauge演示的JavaFX 。
- 带有Nashorn LED读数演示的JavaFX 。
- JavaFX with Nashorn Hello World and 3D demos
- JavaFX with Nashorn Canvas example.
- JavaFX with Nashorn Animated Gauge Demo.
- JavaFX with Nashorn LED readout demo.
这篇关于仅使用JavaScript(Nashorn)进行JavaFX开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!