Java 与 C++ 用于构建具有 C++ 后端的 GUI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3207155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:48:53  来源:igfitidea点击:

Java vs. C++ for building a GUI which has a C++ backend

javac++user-interfacewrapperbackend

提问by sparkFinder

I currently have a C++ backend that I need to connect with a GUI, and since I've never built a GUI before, I was confused on where to start.

我目前有一个 C++ 后端,我需要与 GUI 连接,而且由于我以前从未构建过 GUI,我对从哪里开始感到困惑。

I'm comfortable writing code in C++ and Java, so I'd prefer my GUI to be in one of those languages. Also, the GUI has to be reasonably OS independent over Windows and Linux (and hopefully, hence Macs).

我很习惯用 C++ 和 Java 编写代码,所以我更喜欢我的 GUI 使用其中一种语言。此外,GUI 必须合理地独立于 Windows 和 Linux(希望如此,因此 Macs)的操作系统。

Now I understand that if I use Java to do it, I'll need some wrappers to do it - but I've also heard (strictly second hand) that writing a GUI in C++ is a pain.

现在我明白,如果我使用 Java 来做它,我需要一些包装器来做它 - 但我也听说(严格来说是二手的)用 C++ 编写 GUI 是一种痛苦。

I don't want to rewrite too much of my backend code in Java (who does??) and I was hoping for input on:

我不想用 Java 重写太多我的后端代码(谁做的??),我希望输入:

  • Does either language offer serious advantages/disadvantages compared to the other?
  • How serious is the wrapping issue, and how much rewriting would come in if I used Java.
  • Are there any specific resources I should look at that people think would be relevant?
  • 与另一种语言相比,这两种语言是否有明显的优势/劣势?
  • 包装问题有多严重,如果我使用 Java,会有多少重写。
  • 是否有任何我应该查看的人们认为相关的特定资源?

Thanks and Cheers All :)

感谢和干杯:)

采纳答案by Lena Schimmel

You say you already know C++ and Java, and that you never did a GUI before. That means:

你说你已经知道 C++ 和 Java,而且你以前从未做过 GUI。这意味着:

  • no matter if you go for a Java GUI or a C++ GUI, you will need to learn how to handle the GUI framework
  • if you chose Java, you also need to learn how to interface between the two languages
  • 无论您选择 Java GUI 还是 C++ GUI,您都需要学习如何处理 GUI 框架
  • 如果你选择Java,你还需要学习如何在两种语言之间进行交互

So staying in C++ saves you one thing to learn. Well, it's always a good idea to learn something, but might be a bad idea to learn two new concepts at the same time. Anyway, the learning might be the smaller burden, I guess there is a lot of actual work involed, even when you use tools like SWIG.

因此,留在 C++ 中可以为您节省一件事来学习。嗯,学习一些东西总是一个好主意,但同时学习两个新概念可能是个坏主意。无论如何,学习可能是较小的负担,我猜有很多实际工作涉及,即使您使用像 SWIG 这样的工具。

You might want to know if writing a GUI in Java or doing it in C++ is easier. It depends on the chosen Framework. For Java, you have AWT and Swing which are part of the default Java distribution, and then there is SWT which is used by Eclipse, for example. For C++, there are many toolkits, with Qt, GTK and wxWidgets being the most popular ones, and all three support every major platform. Most of those "C++" GUI toolkits also have a Java binding or even a Java port, so you could use them with Java as well.

您可能想知道用 Java 编写 GUI 还是用 C++ 编写 GUI 更容易。这取决于所选的框架。例如,对于 Java,您有 AWT 和 Swing,它们是默认 Java 发行版的一部分,然后是 Eclipse 使用的 SWT。对于 C++,有很多工具包,其中 Qt、GTK 和 wxWidgets 是最受欢迎的工具包,并且这三个工具包都支持每个主要平台。大多数“C++”GUI 工具包还具有 Java 绑定甚至 Java 端口,因此您也可以将它们与 Java 一起使用。

So far I've used Swing, Qt and a few others which don't help in your situation (The UI thingy that came with Borland C++ Builder and WinForms on .NET). Basically, the concepts are the same for all those frameworks, and I found none of them being harder or easier than the other. The only exception maybe Java, because I never got those LayoutManagers to work, even though the other toolkits have equivalents to LayoutManagers that are easy to master. But maybe thats just me.

到目前为止,我已经使用了 Swing、Qt 和其他一些对您的情况没有帮助的工具(Borland C++ Builder 和 .NET 上的 WinForms 附带的 UI 东西)。基本上,所有这些框架的概念都是相同的,我发现它们中没有一个比另一个更难或更容易。唯一的例外可能是 Java,因为我从来没有让那些 LayoutManagers 工作,即使其他工具包具有易于掌握的 LayoutManagers 等价物。但也许那只是我。

People also will tell you that Java GUIs are always ugly and don't fit the host system. Well, most Java GUIs really are, but IMHO thats not because of Java, but because of bad programming. It takes two lines of code to let a Swing app adapt to the look and feel of the OS, and most programmers simply don't put enough effort into their Java GUIs to copy and paste those two lines... you can imagine how much they care about the rest of their GUI design.

人们还会告诉您 Java GUI 总是丑陋且不适合主机系统。好吧,大多数 Java GUI 确实是,但恕我直言,这不是因为 Java,而是因为糟糕的编程。需要两行代码才能让 Swing 应用程序适应操作系统的外观和感觉,而大多数程序员根本没有在他们的 Java GUI 上付出足够的努力来复制和粘贴这两行……你可以想象有多少他们关心 GUI 设计的其余部分。

For your current situation, I would recommend a C++ GUI, but if you know how your future plans look like, and if you know you will doing Java GUIs for the rest of your life, then it's probably ok to start that now and take the extra effort of .

对于您目前的情况,我会推荐 C++ GUI,但是如果您知道您未来的计划是什么样的,并且如果您知道您将在您的余生中使用 Java GUI,那么现在开始并采取额外的努力。

And if you chose C++ for the GUI, people will tell you all kind of things to pull you in any direction. All of the three big portable frameworks have their pros and their cons, but I don't believe there is any single best or worst one among them. I'd recommend Qt simply because I already used it - but if I'd happten to have used GTK or wxWidgets instead, I'd probably suggest that.

如果你为 GUI 选择 C++,人们会告诉你各种各样的东西,把你拉向任何方向。三大可移植框架各有优缺点,但我不相信其中有任何一个最好或最差。我推荐 Qt 只是因为我已经使用过它 - 但如果我已经使用 GTK 或 wxWidgets,我可能会建议这样做。

回答by Abhay

Have a look at Qt.

看看Qt

In my experience communicating between two different language runtimes is always challenging. If you have a non-trivial application to build the following often pose challenges:-

根据我的经验,在两个不同的语言运行时之间进行通信总是具有挑战性的。如果您有一个非平凡的应用程序来构建以下内容通常会带来挑战:-

  • Error Handling.
  • Memory Management.
  • Multithreading and Synchronization Semantics.
  • 错误处理。
  • 内存管理。
  • 多线程和同步语义。

Apart from increasing one level of indirection due to wrappers, it requires a lot of thinking like circumstances where you need to pass data structures across GUI and backend etc.

除了由于包装器而增加一层间接之外,它还需要大量思考,例如您需要在 GUI 和后端等之间传递数据结构的情况。

For example:- Consider passing a Java Stringfrom GUI to backend C++. Essentially, we have to extract the characters from a Java Stringobject and make them available to the C++ developer without leaking the memory which holds them. This is an example of a basic problem (there are other aspects too like the encoding in which the characters are to be returned).

例如:- 考虑将 JavaString从 GUI传递到后端 C++。本质上,我们必须从 JavaString对象中提取字符,并将它们提供给 C++ 开发人员,而不会泄漏保存它们的内存。这是一个基本问题的示例(还有其他方面,例如要返回字符的编码)。

回答by Thorbj?rn Ravn Andersen

Depending on your needs, a simple web interface might be the simplest when you have no existing frontend code. Embed a tiny web server in your application and open a browser on "http://localhost:12345" (or what port you end up using).

根据您的需要,当您没有现有的前端代码时,一个简单的 Web 界面可能是最简单的。在您的应用程序中嵌入一个小型 Web 服务器并在“ http://localhost:12345”(或您最终使用的端口)上打开浏览器。

回答by mooware

I can't say much about coupling Java and C++, but I suggest you have a look at Qt. It's a C++ library for a lot of things, like file and network access, but it's most famous for GUI development. It also has a nice IDE were you can build your GUI with drag-and-drop. I would also say that Qt is as OS-agnostic as it gets for GUI libraries.

我不能说太多关于 Java 和 C++ 的耦合,但我建议你看看Qt。它是一个用于很多事情的 C++ 库,比如文件和网络访问,但它最著名的是 GUI 开发。它还有一个不错的 IDE,您可以通过拖放来构建 GUI。我还要说 Qt 与 GUI 库一样与操作系统无关。

回答by Edward Strange

Writing a GUI in C++ is no more of a pain than doing it in Java.

用 C++ 编写 GUI 并不比用 Java 编写更痛苦。

There are numerous cross-platform GUI libraries. GTK, gtk--, FoX, WX, etc. I would not recommend Qt since it's not really C++ (uses an extended version of the language requiring a special preprocessor before compilation). Plus it costs a fortune if you don't want to give away your product.

有许多跨平台的 GUI 库。GTK、gtk--、FoX、WX 等。我不推荐 Qt,因为它不是真正的 C++(使用该语言的扩展版本,在编译前需要特殊的预处理器)。此外,如果您不想放弃您的产品,它会花费一大笔钱。

BTW, that's not how the word "hence" is used.

顺便说一句,这不是“因此”这个词的用法。

回答by Andy Thomas

You did not mention the richness of the interaction between front and back ends, which would weight the importance of the existing language in your decision.

您没有提到前端和后端之间交互的丰富性,这会在您的决策中权衡现有语言的重要性。

I've worked with Qt, Swing and SWT, and typically used both C++ and Java code with all of these toolkits. The interaction between languages can add additional cost/risk. However, sometimes that cost is warranted given other benefits.

我使用过 Qt、Swing 和 SWT,并且通常将 C++ 和 Java 代码与所有这些工具包一起使用。语言之间的交互会增加额外的成本/风险。但是,有时考虑到其他好处,该成本是合理的。

If for any reason you choose a Java front end, look at JNAand SWIG.

如果出于任何原因选择 Java 前端,请查看JNASWIG

回答by John

Let's all be honest here. C++ just ain't on the map when it comes to portable GUIs.

让我们在这里说实话。当涉及到可移植 GUI 时,C++ 不在地图上。

Java has a consistent, portable, widely used, thoroughly documented, mature GUI toolkit. C++ has a bunch of half-assed OSS libraries that barely work, none are truly portable, plus some expensive commercial libraries that don't work on all targets they claim to, work spotty on the remaining targets, and invert control so that you are stuck in their weird framework.

Java 有一个一致的、可移植的、广泛使用的、完整记录的、成熟的 GUI 工具包。C++ 有一堆半途而废的 OSS 库几乎不起作用,没有一个是真正可移植的,加上一些昂贵的商业库,它们不能在他们声称的所有目标上工作,在其余目标上工作不稳定,并反转控制,以便您卡在他们奇怪的框架里。

Unless you need C++ for other reasons (of which there are plenty), choose Java for the GUI. The cross-over coding is trivial for someone who knows both languages, but it can get messy to manage, so you'll want to minimize the native interface as best you can. My advice here is to make a pact with your team that you will never attempt to hold pointers (or references) across the interface. It just gets messier if you do, and no debugger can save you when the lines get tangled. Instead, use integer or string keys and pass them across the native interface.

除非出于其他原因(其中有很多)需要 C++,否则为 GUI 选择 Java。对于通晓两种语言的人来说,交叉编码是微不足道的,但管理起来会很麻烦,因此您需要尽可能地最小化本机界面。我的建议是与你的团队达成协议,你永远不会试图在界面上持有指针(或引用)。如果你这样做,它只会变得更加混乱,并且当线路​​缠结时,没有调试器可以拯救你。相反,使用整数或字符串键并通过本机接口传递它们。

回答by Anand

How about eclipse? Looks good and performs well on all platforms. My guess is most of eclipse is Java.

日食呢?看起来不错,在所有平台上都表现良好。我的猜测是大部分 eclipse 是 Java。

回答by Iton Sandal

Why you do not learn native C++ GUI like WINAPI or X11. Then you make console software to compatible one of them like winehq, cygwin or other open sources compatibility or emulated software. Since you never developed GUI on C++. Dont go for Java because make more loaded size to RAM. Unless you have big size of RAM. Eclipse can used 500mb of RAM when running with no project opened.

为什么你不学习像 WINAPI 或 X11 这样的原生 C++ GUI。然后你让控制台软件兼容其中之一,比如 winehq、cygwin 或其他开源兼容性或模拟软件。因为您从未在 C++ 上开发过 GUI。不要选择 Java,因为要为 RAM 增加更多的加载大小。除非你有大内存。Eclipse 在没有打开项目的情况下运行时可以使用 500mb 的 RAM。

回答by jdehaan

  1. Wrapping is not rewriting, it's just an adapter to make both languages meet. It is straight-forward
  2. As you don't seem to be fixed on a language, I would choose a .NET Gui (With C++ CLR) you'll have a GUI that is machine independent and you can easily communicate with your existing code.
  1. 包装不是重写,它只是使两种语言相遇的适配器。这是直截了当的
  2. 由于您似乎并没有固定在一种语言上,我会选择 .NET Gui(使用 C++ CLR),您将拥有一个独立于机器的 GUI,并且您可以轻松地与现有代码进行通信。

For beginners WinForms is maybe easier, but try to use WPF, it's the most modern variant for GUI development in the .NET world.

对于初学者来说,WinForms 可能更容易,但尝试使用 WPF,它是 .NET 世界中最现代的 GUI 开发变体。

Personnally I would use C#/.NET for the GUI and use a C++ CLR wrapper DLL. But it is not the only solution.

就个人而言,我会使用 C#/.NET 作为 GUI 并使用 C++ CLR 包装器 DLL。但这不是唯一的解决方案。

Under Linux, the best .NET implementation is MONO. For all WinForms apps I have developped (they are not the craziest ones), they ran without change. With C++/Qt you will have to recompile for each target OS.

在 Linux 下,最好的 .NET 实现是MONO。对于我开发的所有 WinForms 应用程序(它们不是最疯狂的应用程序),它们运行时没有任何变化。使用 C++/Qt,您必须为每个目标操作系统重新编译。