This chapter provides an overview of the JavaFX™ Script programming language. At a high level, this chapter describes the programming language's main features, saving detailed coverage of specific constructs for subsequent chapters. This book is intended for designers and developers of rich Internet client applications and elements, that run in web pages, as Java™ Web Start software, or as traditional desktop applications. Its content assumes the reader is familiar with either the JavaScript or Java™ programming language, or both. While this document does not define a formal language specification, it can be considered a complete reference for all currently supported language features.
本章提供了JavaFx脚本编程语言的一个概览。本章概述了这门编程语言的主要特性,后续章节将会详细介绍这些特性的细节。本书适用于副Internet客户端应用的设计者和开发者。这些应用可以作为Java Web Start软件运行在网页中或者作为传统的桌面应用。本书假定读者熟悉了javascript或java编程语言或者两者都熟悉。但是本书并不是正式的语言规范,可仅仅做为当前JavaFx支持的语言特性的一个完整的参考。
The JavaFX Script programming language has the following distinctions:
JavaFX脚本编程语言有如下的语言特色:
- Uses a declarative syntax for specifying Graphical User Interface (GUI) components, enabling a developer's code to closely match the actual layout of the GUI.
- 可使用声明式的语法编写图形界面组件,使得开发者的代码非常接近于GUI的实际布局。
- Uses declarative data binding and incremental evaluation, enabling easy creation and configuration of individual components. Application data and GUI components are automatically synchronized.
- 使用声明式的数据绑定和incremental evaluation(不知道如何翻译), 使得创建和配置组件变得简单。应用数据和GUI组件将自动同步。
- Is statically typed, having most of the same code structuring, reuse, and encapsulation features that enable creating and maintaining very large programs in the Java programming language.
- 它是一门静态类型的、绝大部分代码结构、复用和封装特性跟Java编程语言一样,可用来创建和维护大型应用。
- Works with all major IDEs, including the NetBeans IDE, the reference implementation IDE for software development with the Java programming language.
- 支持主流IDE,包括NetBeans IDE,NetBeans IDE是使用Java编程语言进行软件开发的IDE参考实现。
- Is capable of supporting GUIs of any size or complexity.
- 能够支持容易大小和复杂度的GUI。
- Makes it easier to use Swing.
- 它使Swing编程变得更容易
The following sections present a quick tour of the JavaFX Script programming language. These sections provide a general introduction to its core syntax and capabilities, comparing and contrasting to the Java programming language where appropriate. Each topic is then covered in greater detail in subsequent chapters.
接下来的小节是JavaFX脚本编程语言的一个快速指南,介绍了JavaFx的关键语法和能力,并且比较了它跟java编程语言之间的差异,每个主题将在后续的章节中详细的介绍。
In the JavaFX Script programming language, a "script" is one or more declarations or expressions. Evaluating the script evaluates the declarations or expressions, in order:
在JavaFx中,“script”是一个以上的声明活表达式。执行脚本就是顺序执行脚本中的声明或表达式:
var ten : Integer = 10;
java.lang.System.out.println("Twice {ten} is {2 * ten}.");
This prints out:
上面的语句会打印出:
Twice 10 is 20.
Unlike an application written in the Java programming language, a script need not contain any class definitions or functions.
不想用java写的应用,脚本不需要包含class定义或者函数。
Class definitions share many similarities with the Java programming language, but some differences are notable. State, for example, is information stored in attributes, not fields. Behavior is exposed through functions, not methods. The following example defines a simple Rectangle
class that demonstrates the basic syntax of each.
class Rectangle {
attribute width: Integer;
attribute height: Integer;
function grow(): Void {
grow(1);
}
function grow(amount: Integer): Void {
width += amount;
height += amount;
}
}
The JavaFX Script programming language supports multiple inheritance, making it possible to inherit from more than one class.
Classes are covered in detail in Chapter 2
Attributes and Functions are covered in detail in Chapter 3
Object literals provide a simple syntax for class instantiation. The following code creates a single instance of the Rectangle
class defined previously, initializing its width
and height
attributes to 100
. (Note that new
is not needed.)
Rectangle {
width: 100
height: 100
}
To store a reference to this object, use the var
keyword:
var myRect = Rectangle {
width: 100
height: 100
}
Objects are covered in detail in Chapter 2 .
Variables and basic data types are covered in detail in Chapter 4
Expressions and Operators
Like other programming languages, the JavaFX Script programming language supports expressions and operators.
Chapter 5 discusses the expressions and operators available in the JavaFX Script programming language.
A sequence holds an ordered list of objects. This is roughly analogous to Java programming language arrays. Both hold multiple values and are accessed by index starting at 0.
var week = ["Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday"];
var mon = week[0];
var wed = week[2];
var fri = week[4];
Sequence slices are also supported:
var week = ["Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday"];
var weekdays = week[0..4]; // first slice
var weekend = week[5..6]; // second slice
Chapter 6 covers the basics of declaring sequences, while Chapter 7 focuses on using sequences.
Data binding provides a simple syntax for synchronizing the state of multiple objects. When two objects are bound to each other, the second object's value automatically changes whenever the first object is updated. A common use of data binding is to keep GUI components synchronized with their underlying data.
import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.text.Text;
var myString = "Hello World!";
Frame {
width: 50
height: 50
visible: true
stage: Stage {
content: Text {
content: bind myString
}
}
}
// If some other part of code changes myString
// then the GUI's text will automatically change
// as well.
Data Binding is covered in detail in Chapter 8 .
Triggers are blocks of code that run when certain conditions are true. For example, you may want to be alerted if an attribute's value has been set to something that is inappropriate. The following example shows the basic trigger syntax:
import java.lang.System;
ReplaceDemo {
mySensitiveData: "Will anyone notice?"
}
class ReplaceDemo {
attribute mySensitiveData: String
on replace {
System.out.println("I noticed a change!");
};
// application-specific safeguarding code would go here
}
Triggers are covered in detail in Chapter 9 .