Setting system properties in Groovy

社会主义新天地 提交于 2019-12-23 11:52:14

问题


Please note: Although I mention Swing and MacOS here, this question has nothing to do with either of them: I'm just providing them as a concrete example of what I'm trying to do.


I'm trying to set a system property the groovy way. If you are developing a Swing app on a Mac, it is common practice to set the following system property so that your Swing app's menu looks the same as typical Mac apps:

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

When I call that inside my main method, it has the desired effect (the menu bar gets pulled off of the JFrame and is pinned to the top of the screen).

But when I go to try and make that call groovier:

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

it doesn't work. No exceptions, it just stops working and doesn't have the desired effect in the UI. Why, and what can I do to fix it?


回答1:


Should be:

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

or

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

In this piece of code:

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

['apple.laf.useScreenMenuBar', 'true'] is taken as the key. See below:

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

The following piece of code returns correct results:

System.properties['lol'] = 2

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


来源:https://stackoverflow.com/questions/36031930/setting-system-properties-in-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!