简体   繁体   English

在 Java 桌面聊天应用程序中混合使用 Swing 和 JavaFX

[英]Mixing Swing and JavaFX in a Java desktop chat application

I know that there are already some question about this, but I can't find my way!我知道已经有一些关于这个的问题,但我找不到我的方法!

I want to implement a desktop chat application with java which is able to send text,image, video, etc. Now I am using swing component for my chat conversation window.我想用 java 实现一个桌面聊天应用程序,它能够发送文本、图像、视频等。现在我正在为我的聊天对话窗口使用 Swing 组件。 I create a JFrame and add JTabbedPane to it inorder to have tab for each new conversation.我创建了一个JFrame并向其中添加了JTabbedPane ,以便为每个新对话添加选项卡。 for creating each tab I act as follow :为了创建每个选项卡,我的行为如下:

  • create JPanel (I add this to my JTabbedPane as tab)创建JPanel (我将它添加到我的JTabbedPane作为选项卡)

     newtab = new JPanel(); newtab = new JPanel();\nnewtab.setLayout(new BoxLayout(newtab, BoxLayout.PAGE_AXIS)); newtab.setLayout(new BoxLayout(newtab, BoxLayout.PAGE_AXIS));
  • create JTextPane for display's part of the chat (to have style for conversation like android application such as viber, ....)为聊天的显示部分创建JTextPane (具有像 android 应用程序这样的对话风格,如 viber,....)

I want to be able for following styling:我希望能够使用以下样式:

  • diffrent alignment不同的对齐方式
  • change font, color更改字体、颜色
  • insert JComponent (to show other type of messages )插入 JComponent(显示其他类型的消息)
  • setborder of each message round (I don't want the squre one)每个消息轮的设置边界(我不想要方形的)
  • ... ...
context = new StyleContext();
kit = new HTMLEditorKit();
chatPane = new JTextPane();
chatPane.setEditable(false);
chatPane.setContentType("text/html");
chatPane.setEditorKit(kit);
chatPane.setText("");
doc = (HTMLDocument) chatPane.getStyledDocument();
CSS(); // it is for adding ccs style to stylesheet of document
JScrollPane scroll = new JScrollPane(chatPane);
newtab.add(scroll , BorderLayout.CENTER);

My problem is to set perfect stying to my display part, since javax.swing.text.html.CSS provides HTML 3.2 support, so the CSS properties that are supported are limited!我的问题是为我的显示部分设置完美的样式,因为javax.swing.text.html.CSS提供 HTML 3.2 支持,所以支持的 CSS 属性是有限的! while searching on Internet I find JavaFX , but I don't know is it good to use JavaFX and swing together or even is it possible?!在互联网上搜索时我找到了JavaFX ,但我不知道使用JavaFX和一起swing是否好,甚至可能吗?! also which layout manager is better for the JPanel ( newtab ) to have my JTextPane with scroll.还有哪个布局管理器更适合JPanel ( newtab ) 让我的JTextPane滚动。

It IS possible to mix JavaFX and Swing.可以混合使用 JavaFX 和 Swing。 But I have no experience in it.但我没有这方面的经验。 I just did a little FX-UI for a small project which was pretty nice.我只是为一个非常不错的小项目做了一点 FX-UI。 Especially the CSS-Feature is great.尤其是 CSS-Feature 很棒。

According to mixing, I just recently found a blog which discouraged mixing both technologies: http://dlemmermann.wordpress.com/2014/07/17/javafx-tip-9-do-not-mix-swing-javafx/根据混合,我最近发现了一个不鼓励混合这两种技术的博客: http : //dlemmermann.wordpress.com/2014/07/17/javafx-tip-9-do-not-mix-swing-javafx/

Maybe this gives you a little help.也许这会给你一些帮助。

As its already been said, it is possible to mix Swing and JavaFX especially since Java 8 you can do it both ways:正如已经说过的那样,可以混合使用 Swing 和 JavaFX,尤其是在 Java 8 中,您可以同时使用两种方式:

  • Embed Swing Components in JavaFX with SwingNode使用SwingNode在 JavaFX 中嵌入 Swing 组件
  • Embed JavaFX Components in Swing with JFXPanel使用JFXPanel在 Swing 中嵌入 JavaFX 组件

Recently we had to make the same decision.最近我们不得不做出同样的决定。 We had an application and wanted to migrate to JavaFX to get a more modern design and to make use of all the introduced language features like PropertiesBindings etc., which is nicely supported by JavaFX Components.我们有一个应用程序并希望迁移到 JavaFX 以获得更现代的设计并利用所有引入的语言功能,如PropertiesBindings等,JavaFX 组件很好地支持这些功能。

So first we tried to embed JavaFX in Swing.所以首先我们尝试在 Swing 中嵌入 JavaFX。 All new components were embedded with the help of JFXPanel.所有新组件都在 JFXPanel 的帮助下嵌入。 It was really easy, but from time to time we had some rendering issues which got more and more annoying.这真的很容易,但有时我们会遇到一些越来越烦人的渲染问题。 "Unfortunatly" we got used to the new JavaFX API, which is why we deceided redesigning our appliction to make it a JavaFX application with some Swing Parts in it, which was possible, when Java 8 was released, since we didn`t wonna waste time on fixing thoses kind of rendering issues. “不幸的是”我们习惯了新的 JavaFX API,这就是为什么我们决定重新设计我们的应用程序,使其成为一个包含一些 Swing Parts 的 JavaFX 应用程序,这在 Java 8 发布时是可能的,因为我们不会浪费是时候修复那些渲染问题了。 The redesign was actually some work since some concepts are just different.重新设计实际上是一些工作,因为有些概念是不同的。 Benefitting from the new API caused some refactorings, we didnt really wonna do in first place.受益于新的 API 导致了一些重构,我们一开始并没有真正想做。

But then again mixing Swing and JavaFX got a bit fuzzy, and the look and feel of the application didnt really feel convincing, so then we finally removed all Swing Parts and replaced them by JavaFX Components.但是再次混合 Swing 和 JavaFX 变得有点模糊,应用程序的外观和感觉并没有真正令人信服,所以我们最终删除了所有 Swing Parts,并用 JavaFX Components 替换它们。 So far we don`t regret that step, but it was more work then we expected it to be, eventhough we already used patterns like MVP, where only Views had to be refactored, since presenters were (mostly) free from UI stuff (which was really an interesting process, were we learned a lot about MVP and designing an application).到目前为止,我们并不后悔那一步,但它比我们预期的要多得多,尽管我们已经使用了像 MVP 这样的模式,其中只需要重构视图,因为演示者(大部分)摆脱了 UI 的东西(这真的是一个有趣的过程,我们是否学到了很多关于 MVP 和设计应用程序的知识)。

So in conclusion I just can suggest to create a list of views you have and think of all the components you would need and try to find the corresponding components in JavaFX.所以总而言之,我只能建议创建一个您拥有的视图列表,并考虑您需要的所有组件,并尝试在 JavaFX 中找到相应的组件。 Make small examples for the most complex components to see if they fullfill all your usecases.为最复杂的组件制作小示例,看看它们是否能满足您的所有用例。 If that is the case and you still have enough time to switch to JavaFX I personally would go for a pure JavaFX approach, because of the experiences I made with mixing JavaFX/Swing, especially since your UI design seems to be in an early state.如果是这种情况并且您仍然有足够的时间切换到 JavaFX,我个人会选择纯 JavaFX 方法,因为我在混合 JavaFX/Swing 方面的经验,特别是因为您的 UI 设计似乎处于早期状态。 In the end it is just a question of time you have available for your project and if you are really up to learn about the new concepts and components of JavaFX.最后,这只是您的项目可用的时间问题,以及您是否真的准备好了解 JavaFX 的新概念和组件。

Concerning the JavaFX CSS Support, you find a reference here .关于 JavaFX CSS 支持,您可以在此处找到参考。

Recently I was asked same question.最近我被问到同样的问题。 About one month ago I started new project with my team.大约一个月前,我和我的团队开始了一个新项目。 We use Java 8.0 + JavaFX 2.2.我们使用 Java 8.0 + JavaFX 2.2。 What problems did I find in JavaFX?我在 JavaFX 中发现了什么问题?

  • It's new technology, so many issues still not answered.这是一项新技术,许多问题仍未得到解答。 And you must look for it own.你必须自己寻找。
  • No tray supporting, so you must use java.awt.SystemTray .没有托盘支持,所以你必须使用java.awt.SystemTray

Also I found one problem in the design.我还发现了设计中的一个问题。
For example you want to make beautiful list with cells which contains label which stuck to left side and checkbox about right side.例如,您想使用包含粘贴在左侧的标签和右侧的复选框的单元格制作漂亮的列表。 But there is no good way to do it and you have to calculate length of cells and etc..但是没有好的方法可以做到这一点,您必须计算单元格的长度等。

But JavaFX provide great opportunities for cutomizing GUI.但是 JavaFX 为自定义 GUI 提供了很好的机会。 And you can incapsulate your design in jxml file.您可以将您的设计封装在 jxml 文件中。 It's very convient, because it even more separate code from design.这非常方便,因为它甚至将代码与设计分开。

About mixing I think that if platform allows it than you have to use a solution that provided a platform.关于混合,我认为如果平台允许,那么您必须使用提供平台的解决方案。

And I think that the decision to use JavaFX correct, if only because it is a relatively new technology, developed by Oracle and it probably will soon replace the swing.我认为使用 JavaFX 的决定是正确的,因为它是一种相对较新的技术,由 Oracle 开发,它可能很快会取代 Swing。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM