基本类型<o:p></o:p>
Javafx程序语言提供4种基本类型: String, Boolean, Number, and Integer. 这些类型相应的JAVA类型如下:<o:p></o:p>
<o:p> </o:p> | |
JavaFX<o:p></o:p> | Java<o:p></o:p> |
String<o:p></o:p> | java.lang.String<o:p></o:p> |
Boolean<o:p></o:p> | java.lang.Boolean<o:p></o:p> |
Number<o:p></o:p> | java.lang.Number<o:p></o:p> |
Integer<o:p></o:p> | byte,short,int,long,BigInteger<o:p></o:p> |
<o:p> </o:p>
例子:<o:p></o:p>
var s = "Hello";<o:p></o:p>
s.toUpperCase(); // yields "HELLO";<o:p></o:p>
s.substring(1); // yields "ello";<o:p></o:p>
var n = 1.5;<o:p></o:p>
n.intValue(); // yields 1<o:p></o:p>
(1.5).intValue(); // yields 1<o:p></o:p>
s.substring(n); // yields "ello"<o:p></o:p>
var b = true;<o:p></o:p>
b instanceof Boolean; // yields true<o:p></o:p>
当访问到的字段或者返回值来自(或者写入)JAVA方法,都会自动地执行强行转换到数字类型。另外,当Numbers到Integers的转换时将使用强行转换。<o:p></o:p>
<o:p> </o:p>
top<o:p></o:p>
JavaFX能够引入JAVA类, 创建一个新的JAVA对象, 叫他们方法, 并且实现JAVA接口. 下面的程序片段提供一个例子: <o:p></o:p>
import javax.swing.JFrame;<o:p></o:p>
import javax.swing.JButton;<o:p></o:p>
import java.awt.event.ActionListener;<o:p></o:p>
import java.lang.System;<o:p></o:p>
<o:p> </o:p>
var frame = new JFrame();<o:p></o:p>
var button = new JButton("Press me");<o:p></o:p>
frame.getContentPane().add(button);<o:p></o:p>
button.addActionListener(new ActionListener() {<o:p></o:p>
operation actionPerformed(event) {<o:p></o:p>
System.out.println("You pressed me");<o:p></o:p>
}<o:p></o:p>
});<o:p></o:p>
frame.pack();<o:p></o:p>
frame.setVisible(true);<o:p></o:p>
Running the above program displays the following on the screen: <o:p></o:p>
<v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id="_x0000_i1025" style="WIDTH: 92.25pt; HEIGHT: 42.75pt" alt="Image of simple frame window with Press me button." type="#_x0000_t75"><v:imagedata o:href="mhtml:file:///C:\Documents%20and%20Settings\Administrator\桌面\openjfx%20The%20JavaFX%20Programming%20Language.mht!https://openjfx.dev.java.net/images/javafx-lang-defn-files/frame.png" src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.png"></v:imagedata></v:shape><o:p></o:p>
当然, 这不是最好的方法创建 GUI运用 JavaFX. 下面的javafx程序片段达到了同样的效果: <o:p></o:p>
Frame {<o:p></o:p>
content: Button {<o:p></o:p>
text: "Press Me"<o:p></o:p>
action: operation() {<o:p></o:p>
System.out.println("You pressed me");<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
visible: true<o:p></o:p>
}<o:p></o:p>
top<o:p></o:p>
在javafx里, var关键字 传入一个新的变量. 在变量的声明中你或许指定了它的类型,从而使用javafx达到灵活性.如果你不能指定这个类型,javafx将根据它的运用判断它的类型。 A variable declaration takes the form <o:p></o:p>
var variableName : typeName [?,+,*] = initializer;<o:p></o:p>
你可以用其中一个 ?, +,或者* 操作这变量表示的集合, 列表如下: <o:p></o:p>
<o:p> </o:p> | |
Operator<o:p></o:p> | Meaning<o:p></o:p> |
?<o:p></o:p> | 可选,Optional (i.e, may be null)<o:p></o:p> |
+<o:p></o:p> | 一个或者更多One or more<o:p></o:p> |
*<o:p></o:p> | 0或者更多Zero or more<o:p></o:p> |
例如:<o:p></o:p>
var nums:Number* = [1,2,3];<o:p></o:p>
上面的例子: 声明一个变量 nums 它的值定义为由Number类型组成,可出现0次或多次,并且它的值是 [1,2,3]. <o:p></o:p>
这个 :typeName, [?,+,*], 和= initializer(初始化) 一部分声明是可选的, 所以下面的表达方式与上面是相同的: <o:p></o:p>
var nums = [1,2,3];<o:p></o:p>
top<o:p></o:p>