1

我正在使用 UIManager 自定义一些摆动组件,如下例所示,

UIManager.put("TextField.background", COLOR_BG);
UIManager.put("TextField.foreground", COLOR_FG);
UIManager.put("TextField.selectionBackground", COLOR_SB);
UIManager.put("TextField.selectionForeground", COLOR_SF);
UIManager.put("TextField.caretForeground", COLOR_CF);

现在应用程序启动后,我想更改一些颜色,所以我再次调用 UIManager.put 来更新 UIManager,但组件没有更新,我尝试了 repaint()、revalidate()、updateUI(),但没有成功。

谢谢

4

1 回答 1

3

我相信当您将颜色添加到 UIManager 时,您需要将其包装在 ColorUIResource 类中。这允许 UIManager 认为这些是默认 LAF 的一部分,而不是自定义颜色,因此它会在您更新 UI 时重置这些值。

您仍然需要使用:

SwingUtilities.updateComponentTreeUI(...);

阅读 Swing 教程中关于修改外观的部分以获取更多信息。

于 2011-04-26T01:58:10.590 回答