在 Groovy 中设置系统属性

Setting system properties in Groovy

请注意:虽然我在这里提到了Swing和MacOS,但这个问题与他们中的任何一个都无关:我只是将它们作为我正在尝试做的事情的具体示例。


我正在尝试以 groovy 方式设置系统 属性。如果您在 Mac 上开发 Swing 应用程序,通常的做法是设置以下系统 属性,以便您的 Swing 应用程序的菜单看起来与典型的 Mac 应用程序相同:

System.setProperty("apple.laf.useScreenMenuBar", "true")

当我在我的 main 方法中调用它时,它产生了预期的效果(菜单栏从 JFrame 中拉出并固定在屏幕顶部)。

但是当我去尝试让那个调用更时髦时:

System.properties['apple.laf.useScreenMenuBar', 'true']

没用。没有例外,它只是停止工作并且在 UI 中没有达到预期的效果。 为什么,我该如何解决?

应该是:

System.properties['apple.laf.useScreenMenuBar'] = true

System.properties.'apple.laf.useScreenMenuBar' = true 

在这段代码中:

System.properties['apple.laf.useScreenMenuBar', 'true']

['apple.laf.useScreenMenuBar', 'true']作为key。见下文:

def m = [ [1, 2,]:3, 2:4 ]
assert m[1, 2] == 3

下面这段代码returns正确结果:

System.properties['lol'] = 2

assert 2 == System.properties['lol']