Mac OS X 上的 Java Swing GUI

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

Java Swing GUIs on Mac OS X

javauser-interfaceswingmacos

提问by alanlcode

Have you ever attempted using Swing only to end up changing courses because it just couldn't do what you wanted?

您是否曾经尝试过使用 Swing 只是为了改变课程,因为它不能做您想做的事?

I'm pretty new to Swing, having only used it for school projects over 5 years ago, but it seems Swing has come a long way in providing a more native look and feel, so much so that I'm considering using it to develop the GUI for an app on Mac OS X. Before I do, though, I wanted to see if anyone has run into any showstopper issues that prevented them from using Swing.

我对 Swing 还很陌生,5 年前只将它用于学校项目,但似乎 Swing 在提供更原生的外观和感觉方面取得了长足的进步,以至于我正在考虑使用它来开发Mac OS X 上应用程序的 GUI。不过,在我这样做之前,我想看看是否有人遇到过任何阻止他们使用 Swing 的showstopper 问题。

Just off the top of my head, some possibilities:

就在我的头顶,一些可能性:

  • Problems developing custom components that looked "right"
  • Bad interactions with native applications and widgets
  • Performance issues (unresponsiveness, repaint problems)
  • Inability to mimic native behaviors (like Dock interaction)
  • 开发看起来“正确”的自定义组件时出现问题
  • 与本机应用程序和小部件的不良交互
  • 性能问题(无响应、重绘问题)
  • 无法模仿原生行为(如 Dock 交互)

采纳答案by Kevin Montrose

Swing isn't going to give you perfect fidelity with the hosting OS. Sun simply can't devote the considerable resources necessary to do so. My impression is that Swing has gotten much better, but is still going to look out of place by default.

Swing 不会为您提供托管操作系统的完美保真度。Sun 根本无法为此投入大量资源。我的印象是 Swing 变得更好了,但默认情况下仍然会显得格格不入。

The minimum required to even hope to pass as a Mac app:

甚至希望作为 Mac 应用程序通过的最低要求:

  • package your .jar in a .app
  • set the L&F to system default
  • set apple.laf.useScreenMenuBar property to true
    • must do this before any UI code
  • 将您的 .jar 打包到 .app 中
  • 将 L&F 设置为系统默认值
  • 将 apple.laf.useScreenMenuBar 属性设置为 true
    • 必须在任何 UI 代码之前执行此操作

Dock interaction is non-existent in standard Java. You'll have to use Apple's Cocoa-Java bridge, which is no longer supported. I have no idea how tractable JNI is on OS X, which is the only real alternative.

标准 Java 中不存在 Dock 交互。您必须使用不再受支持的 Apple 的 Cocoa-Java 桥接器。我不知道 JNI 在 OS X 上有多容易处理,这是唯一真正的选择。

Performance shouldn't be a problem. Drag & Drop is probably as hairy on OS X as it is everywhere else.

性能应该不是问题。Drag & Drop 在 OS X 上可能和其他地方一样麻烦。

Basically, if you're explicitly targeting OS X you're best off using Objective-C. Its far from impossible to build an app on OS X using Java & Swing, but its a lot of work to make it look "native".

基本上,如果您明确针对 OS X,则最好使用 Objective-C。使用 Java 和 Swing 在 OS X 上构建应用程序远非不可能,但要使其看起来“原生”需要大量工作。

回答by marcolopes

Swing is a nice way to go, or you can go SWT, or BOTH!

Swing 是一种不错的方式,或者您可以选择 SWT,或者两者兼而有之!

But either way, use them on top of the RCP platform. (either with Eclipse or Netbeans).

但无论哪种方式,都可以在 RCP 平台之上使用它们。(使用 Eclipse 或 Netbeans)。

Have a look at this: http://www.adam-bien.com/roller/abien/entry/eclipse_rcp_vs_netbeans_rcp

看看这个:http: //www.adam-bien.com/roller/abien/entry/eclipse_rcp_vs_netbeans_rcp

marco

马可

回答by Kevin Montrose

@Kevin++

@凯文++

Using Cocoa is probably better

使用 Cocoa 可能更好

  • If you want it to look exactly like native applications
  • If you are targeting only the Mac
  • 如果您希望它看起来完全像本机应用程序
  • 如果您仅针对 Mac

If you intend to distribute your applications for Windows, Linux, etc. Swing is a decent choice. It's better but like in any toolkit there are still issues. You'll never get a truly native look and feel with it, same goes for similar UI toolkits which claim to be "cross-platform".

如果您打算为 Windows、Linux 等分发您的应用程序,Swing 是一个不错的选择。它更好,但就像在任何工具包中一样,仍然存在问题。你永远不会得到真正原生的外观和感觉,对于声称是“跨平台”的类似 UI 工具包也是如此。

The Apple Guidelines for Java development can be found here.

可以在此处找到 Apple Java 开发指南。

回答by Csaba_H

As Kevin and John said you should try Objective-C, Cocoa and XCode if you are only targeting Mac users. The developer toolsfor Mac is freely available.

正如 Kevin 和 John 所说,如果你只针对 Mac 用户,你应该尝试 Objective-C、Cocoa 和 XCode。Mac的开发人员工具是免费提供的。

If you want to (or have to) use Java and Swing you can use some libraries to create a GUI that looks well on Macs:

如果您想(或必须)使用 Java 和 Swing,您可以使用一些库来创建在 Mac 上看起来不错的 GUI:

For deploying your application you should read the JarBundlerdocs.

要部署您的应用程序,您应该阅读JarBundler文档。

However, in this case interaction with dock and native applications is very limited.

但是,在这种情况下,与 Dock 和本机应用程序的交互非常有限。

Some other good links are:

其他一些好的链接是: