Overriding LookAndFeel

吃可爱长大的小学妹 提交于 2019-12-13 00:35:42

问题


Hello I am trying to change the colors of my swing progress bar in java. Initially I tried using the following code:

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);

But none of the commands seemed to have any effect!!! Unless I set m_progressBar.stringPainted(true) and then the foreground does change.

Anyhows I know that the problem arises from the following command: UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); which is invoked before the UI is initialized.

So my questions are:
1) Out of curiosity: why does the the stringPainted(true) invocation manages to override foreground UI settings?
2) How do I keep UIManager.setLookAndFeel in the beginning of the code and still override UI settings?

Thank you!!


回答1:


Try this.............

`Changing the Look and Feel After Startup`

You can change the L&F with setLookAndFeel even after the program's GUI is visible. To make 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.

For example:

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

In other words: you can change the look and feel of a top-level container (I don't have tested if it is limited to top-level containers only or if this method can be applied to any container). Or, to answer your question even more precisely: I don't see there's method of styling a single component, but there is a way for single components in (toplevel) containers.



来源:https://stackoverflow.com/questions/12492114/overriding-lookandfeel

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