本文介绍了NetBeans平台:如何以编程方式设置基于Swing的GUI组件的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用NetBeans Platform和JavaFX 11对胖客户端进行编程,其中一项要求是为应用程序提供默认的深色主题.因此,如果应用程序的用户按下深色模式按钮,则应用程序的GUI必须使用深色主题.

I have to program a fat client using NetBeans Platform and JavaFX 11. One of the requirements is to offer a default and dark theme for the application. So if the user of the application press the dark mode button, the application's GUI has to use the dark theme.

虽然对我来说如何设置JFX组件的样式(通过CSS文件)很明显,但是CSS文件对菜单栏之类的NetBeans Platform组件没有影响,因为它们是基于Swing的.例如,场景对象中的JavaFX组件具有深色样式,但NetBeans菜单栏仍然较亮.

While it's obvious to me how to style the JFX components (via a CSS file), the CSS file doesn't have an impact on the NetBeans Platform's components like the menu bar, because they are Swing based. For instance the JavaFX components inside a scene object got a dark styling, but the NetBeans menu bar is still light.

如何以编程方式更改NetBeans Platform组件的样式(就像您在Web开发中使用CSS或在JavaFX中使用CSS支持一样)?

How its possible to change the style of NetBeans Platform's components programmatically (just as you would have done with CSS in web development or with the CSS support in JavaFX)?

推荐答案

平台上有一个嵌入式的深色LAF(至少在我使用的NB12中),称为FlatDarkLaf,看起来不错.

There is an embedded dark LAF in the platform (at least in NB12 which I use) called FlatDarkLaf, it looks nice.

要使用它,您需要在 ModuleInstall 子类的 validate()方法中添加以下代码,以便在启动过程中尽早完成过程.

In order to use it, you need to add the following code in the validate() method of a ModuleInstall subclass, so that it's done very early during the startup process.

NbPreferences.root().node("laf").put("laf", "com.formdev.flatlaf.FlatDarkLaf");
UIManager.installLookAndFeel(new UIManager.LookAndFeelInfo("FlatLaf Dark", latDarkLaf.class.getName()));  

要切换回默认主题,请执行以下操作:

To switch back to the default theme:

NbPreferences.root().node("laf").remove("laf");

我不知道不重新启动即可更改LAF的(简单)方法.

I'm not aware of a (simple) way of changing the LAF without restarting.

有关更完整的示例代码,请在 GitHub 上查看我的应用程序JJazzLab-X.,位于 UISettings Netbeans模块中.

For a more complete sample code have a look at my application JJazzLab-X on GitHub, in the UISettings Netbeans module.

这篇关于NetBeans平台:如何以编程方式设置基于Swing的GUI组件的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:55