How does JavaFX compare to WPF?
I'm mostly a C# programmer, I stopped writing Java about 10 years ago, but I try to keep up with the technology in Java by reading articles, talking with friends, etc.
I've heard about the new rich GUI framework called JavaFX, but couldn't find any resource comparing it with non-Java parallels.
Since I'm very familiar with C# and WPF, I'd like to get a feel about how similar or different the two technologies are.
EDIT: Seeing as there's no answers coming, I'll try to be more specific:
... More to come...
I'm changing this to community wiki so that the comparisons can keep getting updated (hopefully).
I have been learning JavaFX for the last couple of weeks. Here is a high level overview of how it compares to WPF in my eyes:
All of my comments are related to JavaFX 2.0. This information will probably be subject to change as the platform is still fairly immature and is being actively developed.
Graphics
Like WPF, JavaFX uses a retained graphics rendering system. The user interface comprises a scene graph which is composed of 'nodes' which can be thought of as conceptually similar to WPF's UIElement
.
JavaFX will offload the graphics rendering to the GPU if available. The graphics system uses DirectX on Windows and OpenGL on other platforms.
Markup
JavaFX user interfaces can be created both in code and via FXML markup which is similar to XAML in that the object graph can be created by nesting elements.
FXML has some similar features to XAML such as property binding (simple expressions only) and binding to event handlers (any onEvent method). Event handlers can be declared in-line but typically you would bind to an event in the associated controller.
FXML files can have an associated controller which allows you to declare complex event handlers and to set up bindings between properties. This is a controller in the MVC sense and is not the same as a viewModel in the WPF world (typically a controller will have references to nodes and controls).
One difference to WPF is that it appears that the FXML is not compiled into an intermediate binary representation like BAML. I haven't noticed any performance issues yet but have not used the system extensively. I have noticed though, that FXML usually tends to be shorter than any XAML as the platform still encourages you to write code and styles are declared separately.
An introduction to FXML can be found here.
A scene builder is provided free (as in beer), so if you don't like hand coding the UI you can drag and drop elements, set properties and bind to code in your controller and the FXML will be generated automatically. Obviously the scene builder is nowhere near as powerful as Expression Blend but it is still better than the 'designer' provided by Visual Studio.
Binding
JavaFX has a very powerful property and binding system. The Java Bean pattern has been extended to include classes that encapsulate a property (similar to the way WPF dependency properties represent properties). These classes implement interfaces that provide invalidation and change notification.
There is a distinction between invalidation notifications and change notifications. Invalidations just tell you that the binding expression is now invalid and needs to be recalculated; the recalculation does not actually occur until you request the property value via its get()
or getValue()
methods. If you have registered a change listener, however, then the expression will be re-evaluated immediately and anything that is bound to that property will reflect the changes.
JavaFX exposes these properties in a similar way to WPF with a get and set property and a method that returns an instance of the property wrapper (which are not static like WPF properties).
Complex bindings can be created between multiple properties. Want an integer property to be the sum of two others (a = b + c)? No problem, JavaFX provides a Fluent API to express these kind of relationships EG
A.Add(B, C);
If the value of either B or C changes then the appropriate notifications will be raised so that the system knows that A needs to be re-evaluated. Note that in this case, an exception will be thrown if you try and set the value of A as it is bound to the other properties so it does not make sense in this context.
These expressions can be fairly complex EG a = (b + c) * (d - e)
and can include any number of properties. The fluent API is fairly easy to read and use but is not as nice as some of the Fluent API's provided by some of the Microsoft libraries but this is more down to the Java language limitations rather than JavaFX itself.
Simple bi-directional bindings can be created between properties of the same type so that if one is updated the other automatically reflects the change.
JavaFX also provides a low level API to customise bindings yourself if you want to create a custom binding expression that is not provided by the API or if you are concerned about performance.
One of the biggest differences between JavaFX and WPF is that bindings are primarily carried out in code in JavaFX vs. the WPF way of establishing bindings in mark-up.
An introduction to properties and bindings can be found here.
Styles
JavaFX uses CSS to change the looks of the nodes contained in the scene graph. There is a full specification available which explains the types and the properties that can be set on each node type.
JavaFX also provides some additions that help to improve CSS such as variables that can be defined and used elsewhere EG
.button {
my-custom-color: RGB(234, 44, 78);
}
.my-control {
-fx-background-color: my-custom-color
}
It also provides a couple of functions that allow you to derive colours from other previously defined colours which is useful for creating things like gradients. This means a base palette of colours can be defined and the rest can be generated from these values (this is what the default JavaFX CSS file does).
JavaFX CSS does not allow you to define the type of layout used by a node (as of writing this all layout needs to be performed in code). This works really well for me as this was the one aspect of CSS that really caused me pain when using it with HTML.
Personally I prefer CSS to XAML styles which tend to be too verbose for my liking.
A guide to JavaFX CSS can be found here.
Layout
JavaFX provides a number of layout panes that are similar to those provided by WPF. One difference I have noticed is that the measure and layout contract is defined further up the inheritance chain in the Region
class.
As previously mentioned, Layout cannot be carried out using CSS but can be expressed using the code, FXML or created using the scene builder (which is ultimately converted to FXML).
Controls
JavaFX provides an ever growing library of controls that we have come to expect. One major difference between JavaFX and WPF is that the controls are essentially black boxes and cannot be re-templated in the way that WPF controls can. They also seem to expose far less properties than the WPF controls.
The controls do expose some of the implementation specific regions to CSS allowing specific areas of a control to be targetted by your styles. This is known as the substructure of the control. EG a CheckBox
exposes two substructures; the box and the check mark allowing each part of the control to be styled independently. Note that as described earlier only the look of a control can be altered using CSS but the feel cannot. EG you cannot dramatically alter the way a TabPane
lays out its content by altering its internal layout panel in the way you can with the WPF TabControl
.
Whilst this sounds fairly limiting, the preferred way of creating custom controls in JavaFX seems to be using composition along the lines of deriving from a layout panel to position standard controls and re-styling them using CSS.
Conclusion
Overall I am very impressed with what JavaFX has to offer at the moment. Whilst it is no where near as mature as WPF it is being actively developed and Oracle certainly seem to be backing this. Time will tell if it's successful or not.
I would recommend giving JavaFX a try. Read the documentation and try putting together a small application and see what you think.
You should also check out FXExperience.com which is updated regularly with information from the development team.
I think the best way to to get a feel for JavaFX is to just try it out. There are some good tutorials on the JavaFX website. Here's a couple:
They are pretty quick and give you a good feel for the language. There are many others on the JavaFX site if you are interested in more tutorials and articles.
For specific answers to your questions:
Here's another related question.
链接地址: http://www.djcxy.com/p/65912.html上一篇: JavaFX项目
下一篇: JavaFX如何与WPF进行比较?