【问题标题】:Overriding LookAndFeel覆盖 LookAndFeel
【发布时间】:2012-09-19 09:38:04
【问题描述】:

您好,我正在尝试在 java 中更改挥杆进度条的颜色。 最初我尝试使用以下代码:

UIManager.put("ProgressBar.selectionBackground", Color.black);
UIManager.put("ProgressBar.selectionForeground", Color.white);
UIManager.put("ProgressBar.background", Color.white);
UIManager.put("ProgressBar.foreground", Color.RED);
m_progressBar.setBackground(Color.BLACK);
m_progressBar.setForeground(Color.red);

但是这些命令似乎都没有任何效果!!! 除非我设置了m_progressBar.stringPainted(true),然后前景确实发生了变化。

无论如何我知道问题是由以下命令引起的:UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 在 UI 初始化之前调用。

所以我的问题是:
1) 出于好奇:为什么 stringPainted(true) 调用会设法覆盖前台 UI 设置?
2) 如何在代码开头保留UIManager.setLookAndFeel 并仍然覆盖 UI 设置?

谢谢!!

【问题讨论】:

    标签: java swing look-and-feel jprogressbar uimanager


    【解决方案1】:

    试试这个......

    `Changing the Look and Feel After Startup`
    

    You can change the L&F with setLookAndFeel even after the program's GUI is visible. Tomake existing components reflect the new L&F, invoke the SwingUtilities updateComponentTreeUI method once per top-level container. Then you might wish to resize each top-level container to reflect the new sizes of its contained components.

    例如:

    UIManager.setLookAndFeel(lnfName);
    SwingUtilities.updateComponentTreeUI(frame);
    frame.pack();
    

    换句话说:您可以更改顶级容器的外观和感觉(我没有测试它是否仅限于顶级容器,或者此方法是否可以应用于任何容器)。或者,更准确地回答您的问题:我没有看到有样式设置单个组件的方法,但是(顶层)容器中有单个组件的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-11
      • 2012-01-28
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 2020-06-06
      • 2011-01-23
      相关资源
      最近更新 更多