var result;
function run(args : String[]) {
// Convert Strings to Integers
def numOne = java.lang.Integer.parseInt(args[0]);
def numTwo = java.lang.Integer.parseInt(args[1]);
// Invoke Functions
add(numOne,numTwo);
subtract(numOne,numTwo);
multiply(numOne,numTwo);
divide(numOne,numTwo);
}
function add(argOne: Integer, argTwo: Integer) {
result = argOne + argTwo;
println("{argOne} + {argTwo} = {result}");
}
function subtract(argOne: Integer, argTwo: Integer) {
result = argOne - argTwo;
println("{argOne} - {argTwo} = {result}");
}
function multiply(argOne: Integer, argTwo: Integer) {
result = argOne * argTwo;
println("{argOne} * {argTwo} = {result}");
}
function divide(argOne: Integer, argTwo: Integer) {
result = argOne / argTwo;
println("{argOne} / {argTwo} = {result}");
}
<!-- END VTI2 PAGINATION COMPONENT, variation 1 without optional message --><!-- BEGIN VCD1 OVERVIEW COMPONENT -->本课对 JavaFX Script 编程语言进行了实例介绍。您将通过编写一个从命令行运行的简单的计算器来学习有关变量和函数的基础知识。每一节都将介绍一个新的核心概念,对其进行讨论,并提供您可以编译和运行的示例代码。讨论还包含“实际”代码摘录,说明实际的 SDK 演示中是如何使用特定结构的。单击每个演示的链接可以访问 javafx.com Web 站点,该站点提供了完整的代码列表以及开发者的附加注释。 |
<!-- END VCD1 OVERVIEW COMPONENT --> <!-- BEGIN VCD5 ANCHOR LINKS COMPONENT -->目录
<!-- END VCD5 ANCHOR LINKS COMPONENT -->
前一课介绍了如何建立开发环境;这里我们将更深入地研究 calculator.fx 源代码。下面的红色代码声明了程序的脚本变量。脚本变量是使用 var 或 def 关键字声明的。二者之间的区别在于:在脚本的整个生命周期内都可以为 var 变量赋予新值,而 def 变量在被首次赋予新值后将保持不变。这里我们已经为 numOne 和 numTwo 赋予了特定的值,但未对 result 进行初始化,因为此变量将用来存放将来的计算结果:
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
def numOne = 100; def numTwo = 2; var result;
add(); subtract(); multiply(); divide();
function add() { result = numOne + numTwo; println("{numOne} + {numTwo} = {result}"); }
function subtract() { result = numOne - numTwo; println("{numOne} - {numTwo} = {result}"); }
function multiply() { result = numOne * numTwo; println("{numOne} * {numTwo} = {result}"); }
function divide() { result = numOne / numTwo; println("{numOne} / {numTwo} = {result}"); }
|
<!-- END VCD7 CODE SAMPLE COMPONENT -->您可能注意到,我们不需要将这些变量明确指定为存放数值型数据(而不是字符串或任何其他类型的数据)。编译器非常智能,可以根据使用变量的上下文来弄清您的意图。这称为类型推断。类型推断可以使脚本程序员的工作有所简化,这是由于它省去了声明变量与之兼容的数据类型的工作。
实际示例:效果场
上面的屏幕抓图显示了“效果场”演示应用程序。右边的代码摘录显示了该应用程序的几个脚本变量。您还不能理解整个列表,但应该可以根据刚学过的内容看懂突出显示的部分。请记住,尽管本教程的重点仅针对非图形核心结构,但您最终要把这些知识用于自己的基于 GUI 的应用程序。
有关变量的较低层面的讨论,请参见《JavaFX Language Reference》中的 "Chapter 3: Variables"。
在计算器示例中还定义了一些脚本函数,用于对两个数字进行加、减、乘、除。函数是用来执行特定任务的可执行代码块。下面的红色代码定义了四个函数;每个函数执行一种简单的数学计算,然后输出结果。将代码组织成函数是一种常见的做法,这会使程序更易于阅读、使用和调试。函数体通常会缩进以增强可读性。
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
def numOne = 100; def numTwo = 2; var result;
add(); subtract(); multiply(); divide();
function add() { result = numOne + numTwo; println("{numOne} + {numTwo} = {result}"); }
function subtract() { result = numOne - numTwo; println("{numOne} - {numTwo} = {result}"); }
function multiply() { result = numOne * numTwo; println("{numOne} * {numTwo} = {result}"); }
function divide() { result = numOne / numTwo; println("{numOne} / {numTwo} = {result}"); }
|
<!-- END VCD7 CODE SAMPLE COMPONENT -->此外,除非函数代码被明确调用,否则不会执行。这样就可以在脚本的任何位置运行函数。将函数调用放在函数定义之前还是之后无关紧要(在我们的示例源文件中,函数是在实际定义之前调用的):
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
def numOne = 100; def numTwo = 2; var result;
add(); subtract(); multiply(); divide();
function add() { result = numOne + numTwo; println("{numOne} + {numTwo} = {result}"); }
function subtract() { result = numOne - numTwo; println("{numOne} - {numTwo} = {result}"); }
function multiply() { result = numOne * numTwo; println("{numOne} * {numTwo} = {result}"); }
function divide() { result = numOne / numTwo; println("{numOne} / {numTwo} = {result}"); }
|
实际示例:可拖动的 MP3 播放器
在“可拖动的 MP3 播放器”演示中,程序员定义了用于停止或播放当前歌曲的函数。尽管我们正在研究的这些代码完全脱离了上下文,但这些函数的命名选择(stopCurrentSong 和 playCurrentSong)使这些代码是自说明的,因此更易于分析。在对变量和函数进行命名时,请尽量使用这种有意义的字词。约定名称中的第一词全部使用小写字母,后续的每个词的首字母都使用大写字母。
<!-- END VCD7 CODE SAMPLE COMPONENT -->
还可以将脚本函数定义为接受参数。参数是指在调用函数时所传入的特定值。这种方法可使计算器应用程序执行对任何两个数字(而不仅是硬编码到 numOne 和 numTwo 变量中的值)的计算。实际上,在该版本中已完全删除了 numOne 和 numTwo ,仅保留了 result 脚本变量。
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
var result;
add(100,10); subtract(50,5); multiply(25,4); divide(500,2);
function add(argOne: Integer, argTwo: Integer) { result = argOne + argTwo; println("{argOne} + {argTwo} = {result}"); }
function subtract(argOne: Integer, argTwo: Integer) { result = argOne - argTwo; println("{argOne} - {argTwo} = {result}"); }
function multiply(argOne: Integer, argTwo: Integer) { result = argOne * argTwo; println("{argOne} * {argTwo} = {result}"); }
function divide(argOne: Integer, argTwo: Integer) { result = argOne / argTwo; println("{argOne} / {argTwo} = {result}"); }
|
<!-- END VCD7 CODE SAMPLE COMPONENT -->此脚本的输出现在为:
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
100 + 10 = 110 50 - 5 = 45 25 * 4 = 100 500 / 2 = 250
|
<!-- END VCD7 CODE SAMPLE COMPONENT -->实际示例:有趣的照片
在这段来自“有趣的照片”演示的代码摘录中,我们看到一个名为 loadImage 的脚本函数,它可以接受一组参数。同样,函数和参数名称的选择使代码更容易理解。理解该函数的完整实现此时并不重要。重要的是识别可以接受两个参数的函数。当您开始编写自己的应用程序时,您很可能会依赖此类样例代码,从中学习正确的语法。
函数也可能会向调用它的代码返回一个值。例如,可以更改计算器的 add 函数,使其返回每次计算的结果:
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
function add(argOne: Integer, argTwo: Integer) : Integer { result = argOne + argTwo; println("{argOne} + {argTwo} = {result}"); return result;
}
|
第一段红色代码指定函数返回一个 Integer ;第二段红色代码是实际返回值的代码。
<!-- END VCD7 CODE SAMPLE COMPONENT -->
现在,可以按如下方式调用 add 函数:
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
var total;
total = add(1,300) + add(23,52);
|
<!-- END VCD7 CODE SAMPLE COMPONENT -->如果未指定返回值,函数会默认返回 Void 。
实际示例:来自 Flickr 的动画照片
在“来自 Flickr 的动画照片”演示的这段代码摘录中,我们可以看到三个不同的函数使用了返回值。这些返回值比您以前见过的要稍微复杂一点,但核心概念是一样的:每个函数执行某种特定的计算,然后返回一个结果。前两个函数随后调用了一个 Math 函数(用于计算平方根)并返回其结果。第三个函数返回了一个新的 Vector2D 对象。单从此列表来看,没有足够的信息来确切地了解其含义,但借助完整的源代码您就可以看懂它们的含义了(如果您已经首先花时间学习了该语言!)
有关函数的较低层面的讨论,请参见《JavaFX Language Reference》中的 "Chapter 4: Functions"。
最后,脚本也可以接受命令行参数。在计算器示例中,这将使最终用户可以在运行时指定要进行计算的数字。
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
var result;
function run(args : String[]) {
// Convert Strings to Integers def numOne = java.lang.Integer.parseInt(args[0]); def numTwo = java.lang.Integer.parseInt(args[1]);
// Invoke Functions add(numOne,numTwo); subtract(numOne,numTwo); multiply(numOne,numTwo); divide(numOne,numTwo); }
function add(argOne: Integer, argTwo: Integer) { result = argOne + argTwo; println("{argOne} + {argTwo} = {result}"); }
function subtract(argOne: Integer, argTwo: Integer) { result = argOne - argTwo; println("{argOne} - {argTwo} = {result}"); }
function multiply(argOne: Integer, argTwo: Integer) { result = argOne * argTwo; println("{argOne} * {argTwo} = {result}"); }
function divide(argOne: Integer, argTwo: Integer) { result = argOne / argTwo; println("{argOne} / {argTwo} = {result}"); }
|
<!-- END VCD7 CODE SAMPLE COMPONENT -->
此更改引入了 run 函数,脚本通过此函数接收命令行参数。与您以前看到的其他函数不同,run 是用作脚本主入口点的特殊函数。run 函数会将所有命令行参数存储在 args (一个 String 对象序列 )中。(序列是对象的有序列表,与其他编程语言中的数组相似;第 5 课:序列中详细介绍了序列。)
为了运行此脚本,用户现在必须在运行时指定第一个数字和第二个数字:
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
<!-- END VCD7 CODE SAMPLE COMPONENT -->输出现在为:
<!-- BEGIN VCD7 CODE SAMPLE COMPONENT --><!--
<div><b>Lorem Ipsem Dolor</b></div>
<div class="contentdivider">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="grey4">
<tr>
<td><img src="/im/a.gif" width="1" height="4" border="0" alt=" " /></td>
</tr>
</table>
</div>
-->
100 + 50 = 150 100 - 50 = 50 100 * 50 = 5000 100 / 50 = 2
|
|
分享到:
相关推荐
fx:命令行JSON处理工具
Java fx实例教程,有这个实例,不用再买Java FX方面的书都够了
一个简单的Java FX入门级的飞机大战小游戏,能实现飞机的上下左右移动,发射子弹能消灭敌方飞机,有计分系统,当敌方飞机碰到我方飞机时,游戏结束。此游戏没有复杂的贴图,只有矩形来表示飞机和子弹。 适合人群:...
java FX 学习手册 网上收集
Java Fx的开发实例,包含button,TableView,窗体切换等技术,调用CSS样式等,非常好的学习实例
JAVA FX官方教程:RIA应用开发 完整版文档
这是一个ppt给我们介绍了一下java FX的layout
从 javaFx.com 上 下载下来的 喜欢的可以赶紧下
about java FX, u can use it as a description to develop a project with java FX...
用Java fx语言编写 沿着正弦曲线运动的小球的程序吧
漫谈Java FX.pdf
Pro Java FX9 4th [pdf]
java fx 3d 魔方,下载导入项目便可直接使用,RubikFX ,3D ,FX
用java fx写的五星连珠小游戏,适合java初学者
JAVA FX官方教程:RIA应用开发 完整版文档
基于Java FX实现的可视化排序算法,开发工具:Eclipse,JDK版本:1.8, 下载后在eclipse上添加JavaFX 插件就可以使用。演示地址: https://blog.csdn.net/Muscleheng/article/details/82285813
fn-fx, Java FX的功能 API ( fx )这个库提供了一个功能强大的。 目标是通过面向对象的可变JavaFX提供一个"虚拟 DOM"接口。的基本原理尽管web已经经接受了通常在JavaFX中实现的GUI程序的许多方面,但是需要认识到一定...
java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发 小游戏 经典游戏 游戏源码 课程设计 java游戏开发...
基于JAVA FX实现的酒店预订系统网站