JavaFX 1.2 程序迁移指南

分类: JAVAFX技术, JavaFX编程.
标签: , , ,

javafxblogs.com Rss_google Rss_zhuaxia Rss_xianguo


本文系经原作者Stephen Chin授权翻译,英文原文:The Definitive 1.2 Migration Guide,为方便起见,英文原文的副本也可参见本站转载。有关代码迁移和JavaFX 1.2新特性内容,还可以参考JavaFX网站的文章,或者本站文章JavaFX 1.2的新特性。以下译文中的红字部分为译者加注。



本文包含了从JavaFX 1.1迁移程序到JavaFX1.2最常见的变化,例如如语言本身、功能以及API的改变。如果你发现本文没有提到的变化,请在评论中留言。


基本原则

1) JavaFX 1.2 的二进制不兼容性:对于原来的jar文件或者其它第三方的JavaFX库,如JFXtras,必须经过重新编译才能在和JavaFX 1.2的代码兼容,否则会有编译错误。JFXtras将发布基于JavaFX 1.2的更新,新的版本就快就绪了。

2)内部的API将不能用了,如果你的代码包含了impl_或者sun.com的包,你的代码肯定需要修改。

3) 支持Linux和Solaris, 这是应该说是个新功能,这样你可以对用户自豪的宣称支持Linux。译者注:这里的Solaris应指OpenSolaris。


语言上的变化 JavaFX教程、实例、Demo

1)嵌套的块语句中不可以再有变量屏蔽(Shadowing)。原来可以有同名的变量分别处于内层和外层的块语句中,在内层的语句中的变量可以覆盖外层的同名变量。在JavaFX 1.2种,需要修改变量的名字来避免重名。

2)用mixin代替多类继承性。JAVAFX不再支持多类继承性,但是你可以继承一个类和多个mixin类。Mixin就像java中的interface,不同的是它可以有变量和已实现的方法,使得所有子类都可以使用。这点需要的修改也不多,只需要把原来多继承的类加上mixin关键字即可,如:

public mixin class Resizable { /* variables and functions here */ }

译者注:mixin也可以理解成可以多继承的抽象类(abstract class)

3) 在Sequence中,需要用逗号来分隔元素(除了那些用花括号结束元素)。这有点不方便,但实际上是件好事,因为可以避免以前很难追查的语言陷阱。JavaFX 1.2的新特性


Node类的变化 JavaFX编程实例

1)Node.boundsInScene 属性没有了,主要是性能方面的原因,好在有个容易替换且功能相同的调用:

node.localToScene(node.boundsInLocal)


2) Node.focusable 变为 Node.focusTraversable,虽是很小的API变化,但你应知道这点以免碰上编译错误。这是有关focus功能修正的一部分:当某个Node可见(visible)、可聚焦遍历(focusTravserable)并且没被禁用时,可以得到聚焦。(key listener将不再需要)

3) CustomNode.create() 将在 init 块之前调用,原来的方式是create()方法调用在init之后,在postinit之前。既然现在的调用在init之前,那么需要注意的是变量初始化的顺序,可能需要调整一些代码。

4) SwingControl.enabled -> Node.disable 这是一个在Node类中新的标志,统一地控制启用和禁用(enable/disable)。与之矛盾的SwingControl.enabled变量已经去掉了。

5) Rectangle2D -> Bounds 所有Rectangle2D的引用已经改为javafx.geometry.Bounds类了,在代码中可能会产生连锁反应。升级到JavaFX 1.2的简便方法


Layout类的变化 JavaFX Demo Game

1) min/max/pref -> getters 所有Resizable类的属性 (minimumWidth, minimumHeight, maximumWidth, maximumHeight, preferredWidth, preferredHeight) 已被替换为getter函数(getMinWidth, getMinHeight, getMaxWidth, getMaxHeight, getPrefWidth, getPrefHeight respectively)。其中 prefWidth/Height 函数可以代一个额外的参数,即在另一维度的空间大小。在高/宽有约束的layout中,如Flows,会非常有用,但也可以用-1来忽略该参数。

2) impl_layoutX/Y -> layoutX/Y 方法impl_layoutX和impl_layoutY正式替换为layoutX和layoutY变量。建议你使用这组变量来移动node到某个位置,这比translateX/Y要好。

3) impl_requestLayout() -> requestLayout()。 requestLayout 函数类似地从内部转为公开的API.

4) impl_layout -> doLayout()。 原来是用函数类设置layout,现在改为覆盖Parent.doLayout().

5) 增加了Panel类的声明式Layout。如果你在寻找功能上替代Container的方法,可以看看新的Panel类,它可以声明式设置layout,而且,它还能够通过变量覆盖min/max/pref缺省值,从而不用产生Container的子类.

6) Container中的新函数帮助实现布局(layout)。Container类里面有很多新函数,使得很容易来使用新的布局系统。切记要先完全读一遍,因为这样可以使你节约时间,同时可以遵循布局的最佳实践。

7) 不同的变换(transformation)顺序。过去transforms[]变量可让你在layoutBounds中使用变换,而scaleX/translateX/rotate等可以作用在布局之后. 现在transforms[]和transformation变量都统一在布局之后进行计算。


Skin/Control的变化: JavaFX技术应用编程交流

1) Skin.scene -> Skin.node。这个属性名称的变化可以减少易混淆的Scene类所带来的困惑。

2)去掉了Skin.computeXXX的方法。曾经有过一系列的computePreferredWidth/Height等方法,现在可直接用getPrefWidth/Height等函数,参见上文提到的Resizable类。


3)Skin成为抽象类。需要你实现contains和intersects方法。你可以使用新的JFXtras中的AbstractSkin类,或使用以下样板代码:

override function contains(localX:Number, localY:Number):Boolean {
    return node.contains(localX, localY);
}   

override function intersects(localX:Number, localY:Number,
      localWidth:Number, localHeight:Number):Boolean {
    return node.intersects(localX, localY, localWidth, localHeight);
}


4)新的行为(Behavior)类。这个类可以和Control及Skin一起使用来提供可重用的行为。

5)TextBox.text -> rawText, TextBox.value -> text。TextBox类现在重新调整了,text变为rawText,意义不变.


异步处理的变化: JavaFX Guy的博客
1) RemoteTextDocument类没有了。直接用HttpRequest类替代. 译者注:对于原来和后台Server交互的程序要小心这个变化,代码需要调整。

2) AbstractAsyncOperation类没有了。用新的任务基础设施来替代后台Java线程的通信。


动画(Animation)的变化: JavaFX教程、例子、编程论坛

1) Transition.interpolate -> Transition.interpolator。属性名称的改变,使得interpolate标志具有新的含义:可以成为是否做插值运算的开关。

2)Transition现在是继承Timeline类。这是个受欢迎的修改,使得Transitions很容易与Timelines一起使用。因为这个变化,ParallelTransition和SequentialTransition类现在可以有一个sequence保存的Timeline实例。 译者注:这个变化比较有意义,因为原来的Transition也可以作为动画的一种机制,容易令人糊涂。

译者注:以下两点从javafx.com中摘录翻译。

3)KeyFrame中不再提供timelines变量,但是可以用ParallelTransition和SequentialTransition这两个类来实现同样的功能。

4)Transition类中的timelineDirty变量由markDirty()方法替代。


和Java相同的函数: JavaFX Discussion Blog

1)java.lang.Math -> javafx.util.Math。新的JavaFX数学函数库, 使用JavaFX数学库的好处是可以在移动设备上应用(包括pow, log, 等)。

2)java.lang.Properties -> javafx.util.Properties。和Java相似的又一个类,但是可以移植到移动设备上。但需要注意的是,JavaFX的属性文件和Java属性文件格式不一致。


CSS支持的变化:

1)CSS 更新的缺陷(RT-4817)。样式(Stylesheet)的更新不再影响新增的Nodes。同时,你可以强制stylesheet的更新,方法如下:

def senators = bind model.senators on replace {
    delete sceneRef.stylesheets;
    sceneRef.stylesheets = "{__DIR__}styles.css";
}


2) CheckBoxSkin.textFill 不接受样式(RT-4819)。选项标志本身可以使用你所设置的样式(style),但是文本却不行。现在变通的做法是创建没有文本的checkbox,然后在旁边加上一个具有样式和事件处理机制的文本接点(text node)。

3) 不能从CSS中更新control中的skin属性 (RT-4820) 。这曾经是另一个skin支持上的bug。现在可以在代码中实现了。


Production Suite: 作者Henry Zhang的JavaFX技术博客

1) UiStub -> FXDNode。UiStub类现用FXDNode替代了, 增加了更多的功能,包括后台加载(backgroundLoading)和临时替代图像(placeholder images)。 译者注:以下代码示例来自javafx.com:

Scene {
  content: FXDNode {
     url: "{__DIR__}mygraphics.fxz"
     backgroundLoading: true
     placeholder: Text { x: 10 y: 10 content: "Loading graphics ..."}
   }
}


其它:
1) Image.fromBufferedImage()取消了。功能移至SwingUtils.toFXImage()。要记住的是,使用这个函数会在移植代码到移动设备时受到限制。

2) Color.fromAWTColor()取消了。这又是一个AWT痕迹的销除。JavaFX的Color类应该是非常完善的了,所以没有什么理由我们还要继续使用AWT的色彩体系。

3) Duration.toDate没有了。据我所知,应该没有相应的函数,但是我们总是可以获取duration的整数值然后直接操作它。

4) Affine类中不同的变量名称。如果你用到了Affine类, 你的代码很可能需要修改了,因为变量名称换成了更有说明性的方法名,但是功能还是一样的。


评论 (0) 2009年06月14日

JavaFX 1.2 迁移指南(英文)

分类: JAVAFX技术, JavaFX编程.
标签: , , ,

本文经授权转载自Stephen Chin的英文文章The Definitive 1.2 Migration Guide中文翻译参见这里

Below is the JavaFX 1.2 migration guide written by Stephen Chin. The original article can be found on his blog steveonjava.com. I post his article here for readers who cannot visit Stephen’s blog due to networking problem. Steve, thanks a lot for allowing me to share it here. The Chinese translation can be found here.

The Definitive 1.2 Migration Guide

by Stephen Chin


If you have made it this far, you are a serious JavaFX hacker!  This section covers the most common language, functionality, and API changes that you will encounter when migrating applications from JavaFX 1.1.  If you notice anything that is not covered, feel free to add it in the comments and I will update the guide as appropriate.


General:

  • JavaFX 1.2 is not binary compatible – If you use any jars or third-party JavaFX libraries (such as JFXtras), they must be recompiled for the new release or you will get compilation errors.  Speaking of JFXtras, a new version which has been updated for JavaFX 1.2 is nearly ready, and will be announced here when it is complete!
  • All internal-only APIs are broken – More details broken out by section below, but if you rely on any impl_ or com.sun packages, your code will definitely need to be updated.
  • Linux and Solaris are now supported – This is more of a feature than a migration issue, but if you have users on multiple platforms you can now proudly state that Linux is supported.


Language Changes:

  • No more variable shadowing in nested blocks – It previously was valid to name a variable the same in both the inner and outer block of an expression.  The variable in the inner block would simply replace the one in the outer.  However, this will throw a compiler error in JavaFX 1.2.  The fix is simple…  just use a different variable name.
  • Use Mixins instead of Multiple Inheritance – JavaFX no longer supports multiple inheritance.  Instead you can extend one class and as many mixins as you like.  Mixins are like interfaces in Java, except they can have variables and implementations that are shared by all the classes that extend them.  The only change you have to make in your application to start using mixins is to add the mixin keyword when you define a new class like this:
public mixin class Resizable { /* variables and functions here */ }
  • Commas are required in sequences – Sequences now require commas between elements (except those that end in braces).  This is a little less convenient, but actually a very good thing, because it avoids several language pitfalls that were hard to track down before.


Node Changes:

  • Node.boundsInScene is gone – This was removed for performance reasons, fortunately there is an easy replacement that offers the same functionality:
node.localToScene(node.boundsInLocal)
  • Node.focusable -> Node.focusTraversable – Small API change, but good to know when you get the compilation error on focusable not found.  This is part of a larger focus fix where any Node that is visible, focusTraversable, and not disabled will be eligible for focus (a key listener is no longer required).
  • CustomNode.create() gets called before init – Previously the CustomNode create() method would get called after init and before postinit.  Now that it gets called even earlier than init, some of your code may not work correctly.  Make sure that any variables you are depending upon in the create() method are either initialized inline or have a bind declaration so they get updated once set.
  • SwingControl.enabled -> Node.disable – There is a new disable flag on the Node class that is used universally across all controls to enable or disable them.  This conflicted with the old SwingControl.enabled variable, which has now been removed.
  • Rectangle2D -> Bounds – All references to Rectangle2D have been changed to the new javafx.geometry.Bounds class, which may have a ripple effect through your code.


Layout Changes:


  • min/max/pref -> getters – All of the Resizable methods (minimumWidth, minimumHeight, maximumWidth, maximumHeight, preferredWidth, preferredHeight) have been replace with function getters (getMinWidth, getMinHeight, getMaxWidth, getMaxHeight, getPrefWidth, getPrefHeight respectively).  The prefWidth/Height functions take an extra parameter, which is the amount of space in the opposite dimension.  This is very useful for doing height or width constrained layouts, such as Flows, but can be ignored by passing -1.
  • impl_layoutX/Y -> layoutX/Y – The impl_layoutX and impl_layoutY methods have been replaced with official layoutX and layoutY variables.  It is recommended that you use these variables over translateX/Y if you are moving nodes into position.
  • impl_requestLayout() -> requestLayout() – The requestLayout function has similarly moved from an internal to a public API.
  • impl_layout -> doLayout() – Rather than having a function to set the layout on, you now override Parent.doLayout().
  • Panel class added for declarative layouts – If you are looking for a functional alternative to Container, take a look at the new Panel class, which lets you set the layout declaratively.  It also has variables to override the default min/max/pref values without subclassing Container.
  • New functions on Container to help with layouts – There are a whole slew of new functions on the Container class that make it easier to work with the new layout system.  Make sure to read through them, because they will both save you time and help you follow the best practices for layout design.
  • Different transformation order – Previously the transforms[] variable allowed you to apply transformations included in the layoutBounds, while scaleX/translateX/rotate etc. were applied after layout.  Now both transforms[] and the transformation variables are calculated after layout uniformly.


Skin/Control Changes:


  • Skin.scene -> Skin.node – The name of the Node on Skin has changed from scene to node, which helps reduce confusion with the Scene class.
  • Skin.compute* methods removed – There were a series of computePreferredWidth/Height/etc. methods that were removed.  Instead you can directly use the getPrefWidth/Height/etc. methods on the Resizable class mentioned above.
  • Skin is now abstract – The Skin class is now abstract, and required that you implement your own contains and intersects methods.  You can either do this by using the new JFXtras AbstractSkin class or using the following boilerplate code:
override function contains(localX:Number, localY:Number):Boolean {
    return node.contains(localX, localY);
}

override function intersects(localX:Number, localY:Number,
   localWidth:Number, localHeight:Number):Boolean {
    return node.intersects(localX, localY, localWidth, localHeight);
}
  • New Behavior class – There is a new Behavior class that is meant to be used together with Control and Skin to provide reusable behaviors.
  • TextBox.text -> rawText, TextBox.value -> text – There was some swizzling of variables on the TextBox class.  What used to be text changed to rawText, and value now has the same semantics text used to have.


Asynchronous Changes:


  • RemoteTextDocument has been removed – Use HttpRequest directly instead.
  • AbstractAsyncOperation has been removed – Use the new Task infrastructure instead for communcating with Java code running in a background thread.


Animation Changes:


  • Transition.interpolate -> Transition.interpolator – A small name change to make room for a new interpolate flag that allows you to turn on or off interpolation altogether.
  • Transition now extends Timeline - This is a welcome change, which makes it much easier to work with Transitions and Timelines together.  To support this change ParallelTransition and SequentialTransition now take a sequence of Timeliens.


Java Equivalent Functions:

  • java.lang.Math -> javafx.util.Math – There is a new JavaFX math library, which is a drop-in replacement for the Java math library.  The main advantage of using the new JavaFX math library is that all functions are supported on mobile devices (including pow, log, etc.).
  • java.lang.Properties -> javafx.util.Properties – Another class that is similar in functionality to the Java equivalent, but portable to mobile.  Be warned, however, that the JavaFX properties file format is not the same as the standard Java properties file format.


CSS Changes:

  • CSS Update Defect (RT-4817) – Stylesheet updates no longer affect newly added Nodes due to a known defect.  In the meantime, you can do this to force a stylesheet refresh:
def senators = bind model.senators on replace {
    delete sceneRef.stylesheets;
    sceneRef.stylesheets = "{__DIR__}styles.css";
}
  • CheckBoxSkin.textFill doesn’t accept styles (RT-4819) - The check mark will pick up the style you set, but the text won’t.  The workaround for now is to create a checkbox with empty text and add a text node beside it with the correct styling and event handlers.
  • Can’t update the skin property of Control from CSS (RT-4820) – Another bug in the skin support.  You can set this in code instead for now.


Production Suite:

  • UiStub -> FXDNode – UiStub has been replaced with FXDNode, which includes some extra functionality, such as backgroundLoading, and placeholder images.


Miscellaneous:

  • Image.fromBufferedImage() removed – This has moved to SwingUtils.toFXImage().  Remember that if you use this function it will limit portability to mobile devices.
  • Color.fromAWTColor() removed – Yet another AWT-ism that has been removed. The JavaFX Color class is pretty thorough, so there shouldn’t be any reason to work with AWT Colors.
  • Duration.toDate is gone – As far as I know there is no equivalent functionality, but you can always get a duration’s value as a long and manipulate it directly.
  • Different variable names for the Affine class – If you use the Affine class, then your code is likely to break due to the new, more descriptive, method names, but the functionality is the same.


Whew!  …that was quite a lot to take in.  If you are interested to read more about the JavaFX 1.2 release including the new controls, layouts, and other functionality, the best source is to checkout the Pro JavaFX book that Jim, Dean, Weiqi, and I are writing.  It is almost fully updated for the JavaFX 1.2 release, and is available in Alpha right now from the book website.


I want to give a special thanks to Richard Bair and Amy Fowler from the Sun JavaFX team for providing a lot of valuable information on the JavaFX 1.2 release along the way.

Happy hacking on JavaFX 1.2!

评论 (0) 2009年06月13日