Java 桌面应用程序:SWT 与 Swing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2306190/
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-08-13 05:55:49  来源:igfitidea点击:

Java Desktop application: SWT vs. Swing

javaswingswt

提问by janpio

I'm a web developer at day and thinking about building my first real desktop application. The idea is to build a tool that automates a very repetitive task in a web application where no API is available.

我白天是一名 Web 开发人员,正在考虑构建我的第一个真正的桌面应用程序。这个想法是构建一个工具,在没有 API 可用的 Web 应用程序中自动执行非常重复的任务。

I know I want to use Java. I used it before for web stuff, know the syntax pretty well and want the application to be cross plattform as easy as possible.

我知道我想使用 Java。我之前将它用于网络内容,非常了解语法并希望应用程序尽可能简单地跨平台。

Where I'm not so sure is if I should use SWT or Swing. As my main audience uses Windows, I want to look it as native as possible there. Linux and Mac should work, but the looks are not so important here.

我不太确定是应该使用 SWT 还是 Swing。由于我的主要受众使用 Windows,我希望在那里看起来尽可能原生。Linux 和 Mac 应该可以工作,但外观在这里并不那么重要。

So what are the arguments for and against each UI Framework, Swing or SWT?

那么支持和反对每个 UI 框架、Swing 或 SWT 的论据是什么?

Thanks.

谢谢。

PS: I develop on Windows using Eclipse. But was thinking about playing with Netbeans.

PS:我使用 Eclipse 在 Windows 上开发。但是正在考虑玩Netbeans。

采纳答案by josefx

Pros Swing:

优点挥杆:

  • part of java library, no need for additional native libraries
  • works the same way on all platforms
  • Integrated GUI Editor in Netbeans and Eclipse
  • good online tutorials by Sun/Oracle
  • Supported by official java extensions (like java OpenGL)
  • java库的一部分,不需要额外的本地库
  • 在所有平台上以相同的方式工作
  • Netbeans 和 Eclipse 中的集成 GUI 编辑器
  • Sun/Oracle 提供的优秀在线教程
  • 受官方 Java 扩展支持(如 Java OpenGL)

Cons Swing:

缺点摆动:

  • Native look and feel may behave different from the real native system.
  • heavy components (native/awt) hide swing components, not a problem most of the time as as use of heavy components is rather rare
  • 本机外观和感觉可能与真正的本机系统不同。
  • 重型组件(原生/awt)隐藏摆动组件,大多数时候不是问题,因为重型组件的使用相当罕见

Pros SWT:

优点 SWT:

  • uses native elements when possible, so always native behavior
  • supported by eclipse, gui editor VEP (VEP also supports Swing and AWT)
  • large number of examples online
  • has an integrated awt/swt bridge to allow use of awt and swing components
  • 尽可能使用原生元素,所以总是原生行为
  • eclipse支持,gui编辑器VEP(VEP也支持Swing和AWT)
  • 网上大量例子
  • 具有集成的 awt/swt 桥接器,允许使用 awt 和摆动组件

Cons SWT:

缺点 SWT:

  • requires native libraries for each supported system
  • may not support every behavior on all systems because of native resources used (hint options)
  • managing native resources, while native components will often be disposed with their parent other resources such as Fonts have to be manually released or registered as dispose listener to a component for automatic release.
  • 每个受支持的系统都需要本机库
  • 由于使用了本机资源(提示选项),可能无法支持所有系统上的所有行为
  • 管理本机资源,而本机组件通常会与其父级一起处置,其他资源(例如字体)必须手动释放或注册为组件的处置侦听器以自动释放。

回答by Yuval Adam

For your requirements it sounds like the bottom line will be to use Swing since it is slightly easier to get started with and not as tightly integrated to the native platform as SWT.

对于您的要求,听起来最重要的是使用 Swing,因为它更容易上手,而且不像 SWT 那样紧密集成到本机平台。

Swing usually is a safe bet.

摇摆通常是一个安全的赌注。

回答by Buhake Sindi

Interesting question. I don't know SWT too well to brag about it (unlike Swing and AWT) but here's the comparison done on SWT/Swing/AWT.

有趣的问题。我不太了解 SWT(与 Swing 和 AWT 不同),但这是对 SWT/Swing/AWT 的比较。

http://www.developer.com/java/other/article.php/10936_2179061_2/Swing-and-SWT-A-Tale-of-Two-Java-GUI-Libraries.htm

http://www.developer.com/java/other/article.php/10936_2179061_2/Swing-and-SWT-A-Tale-of-Two-Java-GUI-Libraries.htm

And here's the site where you can get tutorial on basically anything on SWT (http://www.java2s.com/Tutorial/Java/0280__SWT/Catalog0280__SWT.htm)

这里是您可以获得有关 SWT 基本上任何内容的教程的站点(http://www.java2s.com/Tutorial/Java/0280__SWT/Catalog0280__SWT.htm

Hope you make a right decision (if there are right decisions in coding)... :-)

希望你做出正确的决定(如果在编码中有正确的决定)... :-)

回答by zeroed

I whould choose swing just because it's "native" for java.

我选择 Swing 只是因为它是 Java 的“原生”。

Plus, have a look at http://swingx.java.net/.

另外,看看http://swingx.java.net/

回答by Ludovico Fischer

An important thing to consider is that some users and some resellers (Dell) install a 64 bit VM on their 64 bit Windows, and you can't use the same SWT library on 32 bit and 64 bit VMs.

需要考虑的一件重要事情是,某些用户和某些经销商 (Dell) 在他们的 64 位 Windows 上安装了 64 位 VM,并且您不能在 32 位和 64 位 VM 上使用相同的 SWT 库。

This means you will need to distribute and test different packages depending on whether users have 32-bit or a 64-bit Java VM. See this problemwith Azureus, for instance, but you also have it with Eclipse, where as of today the builds on the front download page do not run on a 64 bit VM.

这意味着您需要根据用户使用的是 32 位还是 64 位 Java VM 来分发和测试不同的包。例如,在 Azureus 中看到这个问题,但在 Eclipse 中也有它,截至今天,前端下载页面上的构建不能在 64 位 VM 上运行。

回答by nanda

If you plan to build a full functional applications with more than a handful of features, I will suggest to jump right to using Eclipse RCP as the framework.

如果您计划构建具有多个特性的全功能应用程序,我会建议直接使用 Eclipse RCP 作为框架。

If your application won't grow too big or your requirements are just too unique to be handled by a normal business framework, you can safely jump with Swing.

如果您的应用程序不会变得太大,或者您的需求太独特而无法由普通业务框架处理,您可以放心地使用 Swing。

At the end of the day, I'd suggest you to try both technologies to find the one suit you better. Like Netbeans vs Eclipse vs IntelliJ, there is no the absolute correct answer here and both frameworks have their own drawbacks.

归根结底,我建议您尝试两种技术,以找到更适合您的一种。就像 Netbeans vs Eclipse vs IntelliJ 一样,这里没有绝对正确的答案,两个框架都有自己的缺点。

Pro Swing:

职业挥杆:

  • more experts
  • more Java-like (almost no public field, no need to dispose on resource)
  • 更多专家
  • 更像Java(几乎没有公共字段,不需要在资源上进行配置)

Pro SWT:

专业 SWT:

  • more OS native
  • faster
  • 更多操作系统原生
  • 快点

回答by clartaq

I would use Swing for a couple of reasons.

我会使用 Swing 有几个原因。

  • It has been around longer and has had more development effort applied to it. Hence it is likely more feature complete and (maybe) has fewer bugs.

  • There is lots of documentation and other guidance on producing performant applications.

  • It seems like changes to Swing propagate to all platforms simultaneously while changes to SWT seem to appear on Windows first, then Linux.
  • 它存在的时间更长,并且已经对其进行了更多的开发工作。因此,它的功能可能更完整,并且(也许)错误更少。

  • 有很多关于生成高性能应用程序的文档和其他指南。

  • 似乎对 Swing 的更改同时传播到所有平台,而对 SWT 的更改似乎首先出现在 Windows 上,然后是 Linux。

If you want to build a very feature-rich application, you might want to check out the NetBeans RCP(Rich Client Platform). There's a learning curve, but you can put together nice applications quickly with a little practice. I don't have enough experience with the Eclipse platform to make a valid judgment.

如果您想构建一个功能非常丰富的应用程序,您可能需要查看NetBeans RCP(富客户端平台)。有一个学习曲线,但您可以通过一点练习快速地组合出漂亮的应用程序。我对 Eclipse 平台没有足够的经验来做出有效的判断。

If you don't want to use the entire RCP, NetBeans also has many useful components that can be pulled out and used independently.

如果不想使用整个 RCP,NetBeans 也有很多有用的组件可以拉出来独立使用。

One other word of advice, look into different layout managers. They tripped me up for a long time when I was learning. Some of the best aren't even in the standard library. The MigLayout(for both Swing and SWT) and JGoodiesForms tools are two of the best in my opinion.

另一种建议是,查看不同的布局管理器。他们在我学习的时候绊倒了我很长时间。一些最好的甚至不在标准库中。该MigLayout(适用于Swing和SWT)和JGoodies数据表单工具是两个在我看来是最好的。

回答by Karussell

pro swing:

职业挥杆:

  • The biggest advantage of swing IMHO is that you do not need to ship the libraries with you application (which avoids dozen of MB(!)).
  • Native look and feel is much better for swing than in the early years
  • performance is comparable to swt (swing is not slow!)
  • NetBeans offers Matisse as a comfortable component builder.
  • The integration of Swing components within JavaFX is easier.
  • 恕我直言,swing 的最大优点是您不需要随应用程序一起提供库(这避免了数十 MB(!))。
  • 与早年相比,Swing 的原生外观和感觉要好得多
  • 性能堪比swt(swing不慢!)
  • NetBeans 为 Matisse 提供了一个舒适的组件构建器。
  • 在 JavaFX 中集成 Swing 组件更容易。

But at the bottom line I wouldn't suggest to use 'pure' swing or swt ;-) There are several application frameworks for swing/swt out. Look here. The biggest players are netbeans (swing) and eclipse (swt). Another nice framework could be griffon and a nice 'set of components' is pivot (swing). Griffon is very interesting because it integrates a lot of libraries and not only swing; also pivot, swt, etc

但归根结底,我不建议使用“纯”swing 或 swt ;-) 有几个用于 swing/swt 的应用程序框架。看这里。最大的参与者是 netbeans (swing) 和 eclipse (swt)。另一个不错的框架可能是 griffon,而一个不错的“组件集”是pivot(swing)。Griffon 很有趣,因为它集成了很多库,而不仅仅是swing;还有枢轴,swt等

回答by Oliver Watkins

SWT was created as a response to the sluggishness of Swing around the turn of the century. Now that the differences in performance are becoming negligable, I think Swing is a better option for your standard applications. SWT/Eclipse has a nice framework which helps with a lot of boiler plate code.

SWT 是为了应对世纪之交的 Swing 迟缓而创建的。既然性能差异变得可以忽略不计,我认为 Swing 是标准应用程序的更好选择。SWT/Eclipse 有一个很好的框架,可以帮助处理大量样板代码。

回答by incomudro

One thing to consider: Screenreaders

需要考虑的一件事:屏幕阅读器

For some reasons, some Swing components do not work well when using a screenreader (and the Java AccessBridge for Windows). Know that different screenreaders result in different behaviour. And in my experience the SWT-Tree performs a lot better than the Swing-Tree in combination with a screenreader. Thus our application ended up in using both SWT and Swing components.

由于某些原因,某些 Swing 组件在使用屏幕阅读器(以及适用于 Windows 的 Java AccessBridge)时无法正常工作。知道不同的屏幕阅读器会导致不同的行为。根据我的经验,SWT-Tree 与结合屏幕阅读器的 Swing-Tree 相比,性能要好得多。因此,我们的应用程序最终同时使用了 SWT 和 Swing 组件。

For distributing and loading the proper SWT-library, you might find this link usefull: http://www.chrisnewland.com/select-correct-swt-jar-for-your-os-and-jvm-at-runtime-191

为了分发和加载正确的 SWT 库,您可能会发现此链接很有用:http: //www.chrisnewland.com/select-correct-swt-jar-for-your-os-and-jvm-at-runtime-191