2

我对 Swing 很一般,刚开始学习 JavaFX。关于使用哪一个,我对 SO 进行了一些回答,但我仍然无法下定决心。

有没有可能两全其美??

就像,使用 JavaFX 来提供一些视觉上吸引人的图形,并在我想要的时候向 JavaFX 添加一些 Swing ?

4

3 回答 3

3

您可以通过JFXPanel控件在 Swing 应用程序中使用 JavaFX 。请参阅SwingInterOp示例。您还可以从复杂的基于 Swing 的平台(例如NetBeans RCP )内部使用 JavaFX 。

JavaFX/Swing 集成的当前限制是您可以将 JavaFX 组件嵌入到 Swing 应用程序中,但不能将 Swing 组件嵌入到 JavaFX 应用程序中。 JavaFX 嵌入支持中的 RT-12100 Swing 当前计划用于 Java 8。

如果您要开发一个全新的新应用程序,我建议您选择一种技术或另一种技术并坚持使用该技术,或者至少将集成点保持本地化并且很少且相距甚远。这两个框架都很大并且需要时间来学习,而且您将避免任何集成难题,例如需要注意 Swing 处理是在 Swing Event Dispatch 线程上完成的,而 JavaFX 处理是在 JavaFX 应用程序线程上完成的。

JavaFX 还与SWT集成,该集成目前比 JavaFX/Swing 集成更简单(因为只需担心一个 GUI 线程)。

这是来自 Oracle 的 JavaFX/Swing 互操作示例的屏幕截图:

swinginterop 映像

关于使用哪一个,我对 SO 进行了一些回答,但我仍然无法下定决心。

我建议不要这样做,而是选择一个小型应用程序并在 Swing 和 JavaFX 中从头开始实现它,然后看看你喜欢哪个。最好花点时间,因为亲身体验无可替代 - 然后,如果您仍有问题(希望更具体和更有针对性),请返回 StackOverflow 进行研究或提问。

于 2013-03-15T20:31:20.097 回答
2

It depends on your motivation for learning them. If you're looking primarily to boost your CV in the near future and become attractive to businesses, then (for the moment at least) there's many more applications out there written in Swing than JavaFX, purely because the latter is a much newer toolkit.

However, if you're thinking more long term or just want the best tool for the job, personally I find JavaFX to offer more flexibility, be much easier to code in an aesthetically pleasing way, and easier to learn as well (the API behind it is just much nicer.) The only thing against it at the moment is that there's some features that I'd really like to be included, but aren't yet (again, naturally this is what you get by going for the newer toolkit.)

I'd also advise against mixing the two. I'm currently involved in two projects using JavaFX, one entirely JavaFX based and the other a mixture of FX and swing. The latter is much more fiddly to maintain, primarily because of the difference in dispatch threading.

于 2013-03-16T09:21:20.340 回答
2

正如之前的海报所述,您现在可以在 Swing 应用程序中使用 JavaFX。但是,既然 Oracle 已经宣布 JavaFX 是 Java 桌面的未来,为什么不直接学习它。我试图让两者都保持最新,这是一件苦差事。我计划在未来专门专注于 JavaFX——减少自己困惑的机会。

也就是说,JavaFX 似乎仍然不是 JDK 的成熟成员,尤其是在与其他语言结合使用时,例如 Clojure。根据 JavaFX 版本,需要通过不同的机制将其拉入构建工具。但是随着每个版本的发布,这种情况正在变得更好。

于 2013-03-16T15:06:31.327 回答