覆盖外观 [英] Overriding LookAndFeel
问题描述
你好,我试图在Java中更改我的挥杆进度条的颜色. 最初,我尝试使用以下代码:
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);
但是这些命令似乎都没有任何作用!!!
除非我设置m_progressBar.stringPainted(true)
,否则前景会发生变化.
But none of the commands seemed to have any effect!!!
Unless I set m_progressBar.stringPainted(true)
and then the foreground does change.
无论如何,我知道问题是由以下命令引起的:UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
会在初始化UI之前调用它.
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.
所以我的问题是:
1)出于好奇:stringPainted(true)
调用为何设法覆盖前台UI设置?
2)如何在代码开头保留UIManager.setLookAndFeel
并仍然覆盖UI设置?
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?
谢谢!
推荐答案
尝试一下…….......
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.
例如:
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.
这篇关于覆盖外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!