- 分兩大部分:
- Java FX script
- 靜態宣告式script語言
- Java FX Mobile
- 用來開發應用於可攜裝置的平台
- 4個基本型別:
- String (java.lang.String)
- Boolean (java.lang.Boolean)
- Number (java.lang.Number)
- Integer (java.lang.BigInteger,
byte
,short
,int
,long
)
宣告方法: var varName:typeName=value;
var varName:typeName=[itemval1,itemval2,itemval3]; - 邏輯運算子
Java JavaFX
&& and
|| or
! not - 在fx檔將變數寫到std out:
import java.lang.System;
var i:Integer=10;
var str:String="String ddd";
System.out.println("Hello {i} {str}"); - 宣告變數其尾(=號前)可指定下列符號,以限定其集合限制:
- ? 選擇性,可為null(零或一個元數)
- + 一或多個元素
- * 零或多個元素
Ex. var nums:Number* = [5, 9, 13];
- 字串可包於單引號或雙引號間。
- 若要串接字串,相較於java使用+號,java fx在字串中使用大括號,如下:
var name="mark";
var title="Manager";
var complete_name="{title} {name}"; - 在上面的大括號也可以是判斷式,如下:
var truth="1>2 is {if 1>2 then "true" else "false" }";
這種情況下雙引號內有雙引號,是允許的。(相較java不可) - java fx陣列代表一串物件,但陣列本身不是物件。
- sizeof varName 可用來取得varName陣列的包含元素數目。
- 宣告1到100整數陣列:
var myint:Integer=[1..100]; - 陣列操作語法: insert、delete、select 、foreach
- insert
var ints=[1,2,3];
insert 20 into ints; //[1,2,3,20]
insert 30 as first into ints; //[30,1,2,3,20]
insert [100,200] as last into ints; //[30,1,2,3,20,100,200]
insert 99 before ints[.==2]; //[30,1,99,2,3,20,100,200]
insert 88 after ints[5]; //[30,1,99,2,3,88,20,100,200] - delete
var ints=[1,2,3,4,5];
delete ints[.==3]; //[1,2,4,5]
delete ints[.>3]; //[1,2]
delete ints; //[ ] - select
var s:Integer*= select n+n from n in [1,2,3]; //[2,4,6]
var s2:Integer*=select n from n in [1,2,3,4,5,6] where (n%2==0); //[2,4,6]
var s3:Integer*=select n*m from n in [1,2],m in[3,4] ; //[3,4,6,8] - foreach
var s:Integer*= foreach(n in [1,2],m in [3,4]) n*m; - 跳脫java fx關鍵字,將關鍵字放在<< 和 >>之間。
- class宣告:
- 包含有attribute、function、operation
- 可extends多個class
- 範例:(from Learning JavaFX Script, Part 1: An Introduction to JavaFX Script for Java Programmers)
class Person {
attribute name: String;
attribute parent: Person;
attribute children: Person*;
function getFamilyIncome(): Number;
function getNumberOfChildren(): Number;
operation marry(spouse: Person): Boolean;
} - 使用物件:
import java.lang.System;
class X {
attribute a: Number;
attribute b: Number;
}
attribute X.a = 10;
attribute X.b = -1;
var x = new X();
var myXClass = X {
a: 10
b: -1
};import java.util.Date;
import java.lang.System;
var date1 = new Date(95, 4, 23); // call a Java constructor
var date2 = Date { // create the same date as an object literal
month: 4
date: 23
year: 95
};
System.out.println(date1 == date2); // prints true- 大不同:java fx的function和operation主體都在class定義之外。
operation Person.marry(spouse: Person): Boolean {
// Body of operation
}- class定義中一定要有參數和回傳類型,但在class定義外的function或operation主體可省略。
operation Person.marry() {
// Body of operation
} - java fx沒有constructor和setter,取而代之的是使用Trigger:
(object creation、insert、delete、replace) - objectCreation
import java.lang.*;
class X{ attribute serial:Integer*;}
trigger on new X{ insert [10,20] as last into serial}
var x=new X(); - insert
import java.lang.*;
class X{ attribute serial:Integer*;}
trigger on insert num into X.serial{ System.out.println("hi"); }
var x = new X();
insert 12 into x.nums; - delete
import java.lang.*;
class X{ attribute serial:Integer*;}
trigger on delete num from X.serial{ System.out.println("hi"); }
var x = new X();
delete x.serial[1]; - replace
import java.lang.*;
class X{ attribute serial:Integer*;}
trigger on x.serial[olderVal]=newVal{ System.out.println("hi"); }
var x = new X();
delete x.serial[1]; - do{}敘述區塊:
java fx程式是在AWT Event Dispatch Thread (EDT)中執行,而將程式碼放在do區塊中,可使該段程式於背景thread同步執行。 - 增進式評估:使用bind。
部落客廣告聯播
2007年8月1日 星期三
JAVA FX 基礎
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言