如何动态更改 Java LAF UIDefaults?
我正在通过动态更改 UIDefaults 来自定义 Swing 应用程序。 最终目标是更改其中的许多内容(颜色、字体、大小、边框等)并根据每个用户保存结果。 虽然它可能会给应用程序带来一些非标准的外观,但客户提出要求,客户就会收到。
我遇到的唯一问题是 GUI 只会更新一次。 我第一次更改 ui 属性时一切都很好,后续更改不会影响任何内容。
// called from the EDT
// uiKeyName points to some ColorUIResource
UIManager.getDefaults().put(uiKeyName, <<color from color picker>>);
SwingUtilties.updateComponentTreeUI(rootWindow);
它有效一次,但再也不会。 有想法吗?
I'm working on cusomizing a Swing application by dynamically altering the UIDefaults. The end goal is to alter many of them (colors, fonts, sizes, borders, etc) and save the result on a per user basis. While it may give the application some non-standard looks, the client asketh and the client shall receive.
The only problem I'm running into is that GUI will only update ONCE. The first time I change a ui property everthing is great, subsequent changes don't affect anything.
// called from the EDT
// uiKeyName points to some ColorUIResource
UIManager.getDefaults().put(uiKeyName, <<color from color picker>>);
SwingUtilties.updateComponentTreeUI(rootWindow);
It works once, but never again. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将新颜色(例如)放入表中时,它是 Color 对象还是 ColorUIResource? 据我了解,只有当当前值为
null
或UIResource。 因此,如果您插入一个普通的旧 Color 对象,您所做的任何后续更改都将被忽略。此外,正如链接的文档页面所示,这可能仅对字体和前景色/背景色有帮助; 当涉及到更改边框和边距等内容时,您可能会不走运。 这不是 Swing 的可插入 LookAndFeels 的设计目的。 也许您可能会让客户对基于 Synth 的 LAF 感兴趣?
When you put the new color (for example) in the table, is it a Color object, or a ColorUIResource? As I understand it, the new value will only be taken up if the current value is
null
or an instance of UIResource. Thus, if you insert a plain old Color object, any subsequent changes you make will be ignored.Also, as the linked doc page suggests, this may only help with the font and foreground/background colors; when it comes to changing things like borders and margins, you may be out of luck. This is not what Swing's Pluggable LookAndFeels were designed for. Maybe you could interest the client in a Synth-based LAF?