Java GUI 外观变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1165780/
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
Java GUI look and feel changes
提问by ChrisC
I'm new to programming, but I'm preparing to write a Java program. As I'm planning it, I'm trying to find the right GUI for it. I found thispage with GUI options. I have two questions:
我是编程新手,但我正准备编写 Java 程序。在我计划它时,我正在尝试为它找到合适的 GUI。我发现这与GUI选项页。我有两个问题:
- Will these just plug into the Java GUI builder?
- How easy (or hard) is it to change the GUI look and feel after the program is built?
- 这些只是插入 Java GUI 构建器吗?
- 在程序构建后更改 GUI 外观和感觉有多容易(或难)?
回答by Savvas Dalkitsis
Changing the look and feel of a program is as simple as:
更改程序的外观和感觉非常简单:
UIManager.setLookAndFeel("fully qualified name of look and feel");
before any GUI-creating code has been created. So you can easily create all your GUI with your GUI builder and then simply call this at the start of your program.
在创建任何 GUI 创建代码之前。因此,您可以使用 GUI 构建器轻松创建所有 GUI,然后在程序开始时简单地调用它。
(Note that some look and feels, like Substance, are very strict when it comes to threading issues, so you better make sure that all your GUI code is run from the event dispatching thread.)
(请注意,某些外观,例如 Substance,在处理线程问题时非常严格,因此您最好确保所有 GUI 代码都从事件调度线程运行。)
As a suggestion, two very good looking (and professional looking enough) look and feels in my opinion are Substance and also Nimbus which will ship with later releases of the JRE as the default look-N-feel (at least that's the plan) and which can be downloaded separately from java.net.
作为一个建议,在我看来,两个非常好看(而且看起来足够专业)的外观和感觉是 Substance 和 Nimbus,它们将作为默认的外观 N-feel(至少这是计划)随 JRE 的后续版本一起提供和可以从 java.net 单独下载。
So if you opt for Nimbus the code would be:
因此,如果您选择 Nimbus,代码将是:
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
And one example of Substance (Substance has many skins and themes, if you want to know more read their documentation in their site) would be:
Substance 的一个例子(Substance 有许多皮肤和主题,如果你想了解更多,请阅读他们网站上的文档)是:
UIManager.setLookAndFeel(new org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel());
Also note that you can use the cross platform skin (Metal) like this:
另请注意,您可以像这样使用跨平台皮肤(金属):
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
And finally if you are on windows you can use the system look and feel like this:
最后,如果您在 Windows 上,您可以像这样使用系统外观:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
回答by Powerlord
Building on what Uri said, you may want to stick with one of the more well-known look and feels.
以 Uri 所说的为基础,您可能想要坚持使用一种更知名的外观和感觉。
If you use Swing, you may want to look into using the Nimbuslook and feel... it's included with Java 6u10 and newer. Nimbus is built on the Synthframework included with Java 5 and newer.
如果您使用 Swing,您可能需要考虑使用Nimbus 的外观和感觉……它包含在 Java 6u10 和更新版本中。Nimbus 构建在Java 5 和更新版本中包含的Synth框架之上。
Of course, if the end user is using a lower version, it will throw an UnsupportedLookAndFeelException and default to whatever the JVM default is (usually the Metal/Ocean (cross-platform) theme).
当然,如果最终用户使用的是较低版本,它将抛出 UnsupportedLookAndFeelException 并默认为 JVM 默认值(通常是 Metal/Ocean(跨平台)主题)。
As a side note, if you use Swing, you can switch which look and feel is being used on the fly. You just have to call
附带说明一下,如果您使用 Swing,您可以即时切换正在使用的外观和感觉。你只需要打电话
// "lnfName" is the look and feel name.
// "frame" is the window's JFrame.
// A try/catch block is omitted here.
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();
回答by basszero
Ignoring the pro/con of choosing a look and feel (LAF), I'll add some of my experience which using them...
忽略选择外观(LAF)的利弊,我将添加一些使用它们的经验......
If you're using a third-party/cots componentslibrary (JIDE, SwingLabs/SwingX, etc.) you will have problems as UI classes won't always exist or be up to date with all of the LAFs. JIDE supports a few external LAFs (Alloy and Synthetica). Some LAFs also offer support for widgets; Substance supports some of JIDE and SwingX. The end result is an application that is partially skinned. Not pretty. Make sure your LAF supports all of you components. If you're pure Swing/AWT, no problems.
如果您使用第三方/cots 组件库(JIDE、SwingLabs/SwingX 等),您将遇到问题,因为 UI 类不会始终存在或与所有 LAF 保持同步。JIDE 支持一些外部 LAF(Alloy 和 Synthetica)。一些 LAF 还提供对小部件的支持;Substance 支持部分 JIDE 和 SwingX。最终结果是一个部分皮肤的应用程序。不漂亮。确保您的 LAF 支持您的所有组件。如果您是纯 Swing/AWT,那没问题。
In light of this, we use Alloy for JIDE support, considering moving to Synthetica. We would love to use Substance, but it doesn't have any JIDE support.
鉴于此,我们将 Alloy 用于 JIDE 支持,并考虑转向 Synthetica。我们很想使用 Substance,但它没有任何 JIDE 支持。
回答by Uri
Generally speaking, third-party GUI libraries with "look and feel" are not very popular, which is possibly an indication of their quality.
一般来说,具有“外观和感觉”的第三方 GUI 库不是很受欢迎,这可能是其质量的一个标志。
Standard GUI libraries are inherently very complex, and while they support some look and feel tweaking, the tweaks are often minor or constrained.
标准 GUI 库本质上非常复杂,虽然它们支持一些外观和感觉调整,但这些调整通常很小或受到限制。
I would recommend sticking to Swing or SWT, and seeing what you can do with their look and feel mechanism, but don't expect miracles.
我建议坚持使用 Swing 或 SWT,看看你能用它们的外观和感觉机制做什么,但不要指望奇迹。

